Return Array from Mysql db Problem

Have been trying to hack an existing javascript scroller to display items from a mysql db using php. I know just enough php to be dangerous (but I’m learning) Anyway, here is the situation.
dbsetup : One table with 8 rows, 3 fields per row, “id” “content” “status”
Script Function: Contect to db, Grab “content” from all rows where “status”=active, append this to some std javascript calls in a given format and make it available to the rest of the javascript.
Problem: The javascript requires the data being pulled from the db to be in the following format:

[code]ITEMS = [ {‘file’: ‘’,‘content’: ‘Some Content’,‘pause_b’: 2,‘pause_a’: 0},

{‘file’: ‘’,‘content’: 'Some other Content, ‘pause_b’: 2,‘pause_a’: 0} ]
Notice there is no comma after the last item before the closing bracket.

The output of my php looks like this (Typical):

ITEMS = [ {'file': '','content': 'Some Content', 'pause_b': 2,'pause_a': 0},

{'file': '','content': 'Some Other Content', 'pause_b': 2,'pause_a': 0},

{'file': '','content': 'Some Other Content Again', 'pause_b': 2,'pause_a': 0},

{'file': '','content': 'Yet Some More Content', 'pause_b': 2,'pause_a': 0},]

Notice the comma after the last item…

Rest of the php works fine, and as a matter of fact the scroller javascript works with this output in FF but unfortunately IE hangs with the last comma. Here is the php I am using now.

$host = 'mydbhost';
$user = 'iusername';
$pass = 'pswd';
$name = 'dbname';
$cnn = mysql_connect($host, $user, $pass) or die ('Error connecting to mysql');
$query = "SELECT * FROM ts WHERE status='active' ORDER BY id_num ASC LIMIT 8";
$result = mysql_query($query) or die('Error');
$num_rows = mysql_num_rows($result);


var LOOK = {
    // scroller box size: [width, height]
    'size': [120, 110]

    // autoscroll - true, on-demand - false
    'auto': true,
    // vertical - true, horizontal - false
    'vertical': true,
    // scrolling speed, pixels per 40 milliseconds;
    // for auto mode use negative value to reverse scrolling direction
    'speed': 2

while($row = mysql_fetch_object ( $result ) ){
if($i == $num_rows){
echo "{'file': '','content': '$row->content','pause_b': 2,'pause_a': 0}";
echo "{'file': '','content': '$row->content','pause_b': 2,'pause_a': 0},";

Problem is how to eliminate the last comma while leaving the rest? Think it should be simple, but like the jitterbug, it plumb evaded me. :(

David ONeill

Problem Solved :D

New Code:

[code]ITEMS = [

<?php while($row = mysql_fetch_object ( $result ) ){ $res[] = "{'file': '','content': '$row->content','pause_b': 2,'pause_a': 0}"; } echo(implode(",",$res)); mysql_free_result($result); mysql_close(); ?>][/code]

You can use implode to easily add the comma between rows. $res[] means to append this string to the $res array.

Tks to “UNIFLARE” at

Hi Capt,
Just as an FYI. I think your original code was failing do to a mismatch in the $i var and the number num_rows was returning. $i starts at 0 and num_rows is equal to the literal number of rows returned. So you would need to have the condition of your if be :
while($row = mysql_fetch_object ( $result ) ){
if($i == ($num_rows - 1)){
echo “{‘file’: ‘’,‘content’: ‘$row->content’,‘pause_b’: 2,‘pause_a’: 0}”;
echo “{‘file’: ‘’,‘content’: ‘$row->content’,‘pause_b’: 2,‘pause_a’: 0},”;

Might prevent a problem in the future.

Sponsor our Newsletter | Privacy Policy | Terms of Service