Dysan Posted November 23, 2007 Share Posted November 23, 2007 Why do I get the following error message, when output items from the database? Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\User\Desktop\Xampp\htdocs\1.php on line 17 foreach($array as $item) { $result = mysql_query("SELECT * FROM names WHERE id = '$id'"); while($row = mysql_fetch_array($result)) //This is line 17 { $row['Firstname']." - ". $row['Surname'] ." ".$row['Sex']; } } Link to comment https://forums.phpfreaks.com/topic/78577-code-not-working/ Share on other sites More sharing options...
GingerRobot Posted November 23, 2007 Share Posted November 23, 2007 This means there is either something wrong with your query, or there are no results. Try this: <?php foreach($array as $item){ $result = mysql_query("SELECT * FROM names WHERE id = '$id'") or die(mysql_error()); $num = mysql_num_rows($result); if($num > 0){ while($row = mysql_fetch_array($result)){ $row['Firstname']." - ". $row['Surname'] ." ".$row['Sex']; } } } ?> Link to comment https://forums.phpfreaks.com/topic/78577-code-not-working/#findComment-397582 Share on other sites More sharing options...
helraizer Posted November 23, 2007 Share Posted November 23, 2007 Ginger's code might work but normally for the mysql_query you should probably do it like: <?php $sql = "SELECT * FROM `names` WHERE `id`='$id'" $result = mysql_query($sql) or die(mysql_error()); ?> I think you'll find that that should work (haven't tried it but it should) Link to comment https://forums.phpfreaks.com/topic/78577-code-not-working/#findComment-397588 Share on other sites More sharing options...
revraz Posted November 23, 2007 Share Posted November 23, 2007 Tick marks shouldn't make any difference in this case. Link to comment https://forums.phpfreaks.com/topic/78577-code-not-working/#findComment-397595 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.