I am cycling through a return set and trying to capture the result in a concatenated string. I have verified that I get a valid return value and also verified the quantity in the return value.
This works

while ( $i < $num) {
    $bone .=  "<item> <record_num>" . mysql_result($result, $i,'record_num') . "</record_num>" .
		 		"<date_field>" .      mysql_result($result, $i, 'date_field') . "</date_field>" .
		 		"<description>" .     mysql_result($result, $i, 'description') . "</description> </item>";
                $i++;

This does not work

while ($row = mysql_fetch_row($result)){
 $bone .=  "<item> <record_num>" . $row['record_num'] . "</record_num>" .
		 		"<date_field>" .   $row['date_field'] . "</date_field>" .
		 		"<description>" .  $row['description'] . "</description> </item>";
}

In the code that does not work I get all the information but the actuall variable, $row[‘date_field’] and the like. I have tried mysql_fetch_rows, mysql_fetch_arrays, and mysql_fetch_assoc.
Here is an example of what is returned.

<Item> <record_num></record_num><date_field></date_field><description></description> </item>
<item> <record_num></record_num><date_field></date_field><description></description> </item>
<item> <record_num></record_num><date_field></date_field><description></description> </item>
…

Please point out what I am doing incorrectly.

Recommended Answers

All 2 Replies

The same thing can be done this way properly -

<?
$bone  = '';
while($row = mysql_fetch_array($executed_q))
{
    $bone .=  "<item> <record_num>" . $row['record_num'] . "</record_num>" .
		 		"<date_field>" .$row['date_field'] . "</date_field>" .
		 		"<description>" .$row['description'] . "</description> </item>";
}
?>

provided you have 'record_num' , 'date_field' and 'description' as column names in your table.
and $executed_q is the return from your executed query, like
$executed_q = mysql_query('Your query');

Your example is what I was doing given different variable names. I did not check out google, but I have been reading the PHP manual.
I resolved the issue by using UPPER CASE for the variable fields after noticing the spelling in the debugger.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.