Jump to content

Code Not Working!


Dysan

Recommended Posts

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

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

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

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.