# Return Array from Mysql db Problem

**URL:** <https://www.phphelp.com/t/return-array-from-mysql-db-problem/11165>\
**Category:** Beginners - Learning PHP\
**Created:** [April 9, 2008, 1:28am UTC](https://www.phphelp.com/t/return-array-from-mysql-db-problem/11165 "2008-04-09T01:28:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![captdavid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/e480ec/32.png) [@captdavid](https://www.phphelp.com/u/captdavid)\
**Post date:** [April 9, 2008, 1:28am UTC](https://www.phphelp.com/t/return-array-from-mysql-db-problem/11165/1 "2008-04-09T01:28:07Z")

</div>

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} ]  
[/code]  
Notice there is no comma after the last item before the closing bracket.

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

```auto
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.

```auto
<?PHP
$host = 'mydbhost';
$user = 'iusername';
$pass = 'pswd';
$name = 'dbname';
$cnn = mysql_connect($host, $user, $pass) or die ('Error connecting to mysql');
mysql_select_db($name);
$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]
},

BEHAVE = {
    // 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
},

ITEMS = [
<?php
while($row = mysql_fetch_object ( $result ) ){
if($i == $num_rows){
echo "{'file': '','content': '$row->content','pause_b': 2,'pause_a': 0}";
}else{
echo "{'file': '','content': '$row->content','pause_b': 2,'pause_a': 0},";
$i++;
}
}
mysql_free_result($result);
mysql_close();
?>]
```

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. ![:(]()

TIA  
David ONeill

---

<div class="post-metadata">

**Author:** ![captdavid](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/e480ec/32.png) [@captdavid](https://www.phphelp.com/u/captdavid)\
**Post date:** [April 9, 2008, 2:49am UTC](https://www.phphelp.com/t/return-array-from-mysql-db-problem/11165/2 "2008-04-09T02:49:18Z")

</div>

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 [PHPFreaks.com](http://PHPFreaks.com)

---

<div class="post-metadata">

**Author:** ![gordonk66](https://www.phphelp.com/letter_avatar_proxy/v4/letter/g/9fc29f/32.png) [@gordonk66](https://www.phphelp.com/u/gordonk66)\
**Post date:** [April 9, 2008, 4:30am UTC](https://www.phphelp.com/t/return-array-from-mysql-db-problem/11165/3 "2008-04-09T04:30:04Z")

</div>

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}”;  
}else{  
echo “{‘file’: ‘’,‘content’: ‘$row-\>content’,‘pause\_b’: 2,‘pause\_a’: 0},”;  
$i++;  
}  
}

Might prevent a problem in the future.
