Jump to content

php loop problem


pouncer

Recommended Posts

$results = mysql_query("SELECT * FROM $cat WHERE item_id='$item_id'");
$num = mysql_num_fields($results);
$row = mysql_fetch_row($results);

$i = 0;

while ($i < $num) {
$name = mysql_field_name($results, $i);
$data = $row[$name];

echo $name . " - " . $data;
echo "<br>";

$i++;
};

 

It just echos the field names, but not the data:

 

item_id -

image_URL -

title -

media -

director -

cast -

description -

 

why is this?

Link to comment
https://forums.phpfreaks.com/topic/42109-php-loop-problem/
Share on other sites

I do not know why you don't just call the array by the column name, but this should help you on your way.

 

$results = mysql_query("SELECT * FROM $cat WHERE item_id='$item_id'");

$i=0;
while ($row = mysql_fetch_array($results)) {
$num = count($row);

while ($i < $num) {
$name = mysql_field_name($results, $i);
$data = $row[$name];
echo $name . " - " . $data;
echo "<br>";
$i++;
}
}
?>

 

--FrosT

Link to comment
https://forums.phpfreaks.com/topic/42109-php-loop-problem/#findComment-204240
Share on other sites

ok thanks it prints the all now but i get error

 

Warning: mysql_field_name() [function.mysql-field-name]: Field 7 is invalid for MySQL result index 7 in

Warning: mysql_field_name() [function.mysql-field-name]: Field 7 is invalid for MySQL result index 8 in

etc.. etc.. up to index 12

 

line 214 which is

 

$name = mysql_field_name($results, $i);

 

?

Link to comment
https://forums.phpfreaks.com/topic/42109-php-loop-problem/#findComment-204250
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.