Jump to content

mysql_fetch_array need your help !

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Ahmos

Ahmos

    Newbie

  • Members
  • PipPip
  • 12 posts
hi all
am beginner in php and need your help
in the code i have error

Quote

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\AppServ\www\db\db.php on line 11
the code
<? 

$conn = mysql_pconnect('localhost','root','123456');

if(!$conn ){echo mysql_error($conn);}

$db=mysql_select_db('info',$conn);


$sql="SELECT user_name FROM users";

$result=mysql_query($sql,$conn);




while($row= mysql_fetch_array( $result ))

{

	echo $row["user_name"];

	echo "<br>";

	echo $row["user_id"];echo "<br>";

}


?>
note my database name is info and table name is users and assume password 123456

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Your query is returning an error, and thus becomes "false" and cannot be extracted with mysql_fetch_array.

You must check mysql_error() if $result is false.

i.e.
$result=mysql_query($sql,$conn); [COLOR=#000000][COLOR=#007700]
[/COLOR][/COLOR]
if($result == false) {
  echo mysql_error();
  exit;
}

There are two issues with your code:

1: You should use mysql_fetch_assoc to return an associative array, rather than an array of numerical index
2: You do not ask for user_id in your query, so you cannot use it in your while loop. You must also select it, "SELECT user_id,user_name FROM ..."
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Ahmos

Ahmos

    Newbie

  • Members
  • PipPip
  • 12 posts
thanks Alexander ,
i will search for mysql_fetch_assoc:blink:
and i will test it :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users