pouncer Posted March 10, 2007 Share Posted March 10, 2007 $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 More sharing options...
per1os Posted March 10, 2007 Share Posted March 10, 2007 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 More sharing options...
pouncer Posted March 10, 2007 Author Share Posted March 10, 2007 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 More sharing options...
pouncer Posted March 10, 2007 Author Share Posted March 10, 2007 i just went back to my code and used $row = mysql_fetch_assoc($results); Link to comment https://forums.phpfreaks.com/topic/42109-php-loop-problem/#findComment-204261 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.