Forum Moderators: coopster
Im still new to php.
I need a function to my website, where I call my DB and for each row returned, values are stored as variables.
The variables are sent to my webpage (in flash) for later use.
Example:
2 rows in the DB:
id=1, name=joe, date=01.03
id=2, name=al, date=02.04
The variables defined should then be:
id1=1
id2=2
name1=joe
name2=al
date1=01.03
date2=02.04
If there where 3 rows the variables should be something like:
id1=1
id2=2
id3=3
... etc etc.
I have made the connection to the DB, retrieve 1 row, and make the variables for the 1 row. But I dont know how to make the function so it assigns the variables for each row in the DB.
Any suggestions or links to tutorials etc.?
Thanks in advance.
You may consider building something on the fly. I'm not sure how you're getting the data to Flash, but here's one example, in which you build an XML string as you traverse the rows:
$xml=NULL;
$query = "select id,name,`date` from table";
// Note that "date" is a very bad name for a database
//table field, it is a data type
$result = mysql_query($query);
while ($row=mysql_fetch_array($result)) {
$xml .= '<userid="' . $row['id'] . '" name="' . $row['name'] .
'" date="' . $row['date'] . '">';
}
if ($xml) {
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<userData>' .
$xml .
'</userData>';
}
You would then proceed to write the XML file out to a file in a location known by your Flash.
Note that $row, and it's subsequent members ($row['id'], $row['name'] and $row['date']) are overwritten each time the loop is traversed and only the last record's value will exist after the while loop. We use them, repopulate them, then we're done with them.
This is why you might not want to create persistent variables that live after the loop, this will make for a memory hog if you have thousands of variables.
-----
$sql = 'SELECT * FROM table_name ORDER BY id ASC';
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
for($i = 0; $i < $rows; $i++) {
$entryid[$i] = mysql_result($result,$i,"id");
$entryname[$i] = mysql_result($result,$i,"name");
$entrydate[$i] = mysql_result($result,$i,"date");
} And you can then call a variable by saying:
$entryid[3] Which will get you the id for the third entry into the database.