Connecting Tech Pros Worldwide Forums | Help | Site Map

Supplied argument is not a valid MySQL result resource.

Newbie
 
Join Date: Feb 2009
Posts: 3
#1: Feb 5 '09
Hi I am new to Php.I am trying to read My sql database data from table to php.

I am getting this warning

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource

and my code is
Expand|Select|Wrap|Line Numbers
  1.       <?php
  2.  
  3.       $con = mysql_connect("localhost","sandman","sandcastle");
  4.  
  5.       if (!$con)
  6.  
  7.       {
  8.  
  9.       die('Could not connect: ' . mysql_error());
  10.  
  11.       }
  12.  
  13.       print "Connected to MySQL<br>";
  14.  
  15.  
  16.  
  17.       mysql_select_db("sandbox", $con);
  18.  
  19.  
  20.  
  21.       $result = mysql_query("SELECT parcel,location from accessor");
  22.  
  23.  
  24.  
  25.       echo "<table border='1'>
  26.  
  27.       <tr>
  28.  
  29.       <th>Parcel</th>
  30.  
  31.       <th>Location</th>
  32.  
  33.         </tr>";
  34. while($row = mysql_fetch_array($result))
  35.  
  36.       {
  37.  
  38.       echo "<tr>";
  39.  
  40.       echo "<td>" . $row['parcel'] . "</td>";
  41.  
  42.       echo "<td>" . $row['location'] . "</td>";
  43.  
  44.  
  45.  
  46.       echo "</tr>";
  47.  
  48.       }
  49.  
  50.  
  51.       echo "</table>";
  52.  
  53.  
  54.  
  55.       mysql_close($con);
  56.  
  57.       ?>
  58.  
plz let me know where I am doing wrong

Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,660
#2: Feb 5 '09

re: Supplied argument is not a valid MySQL result resource.


I guess the query failed and thus returned false (which is not a valid resource).
Newbie
 
Join Date: Feb 2009
Posts: 3
#3: Feb 5 '09

re: Supplied argument is not a valid MySQL result resource.


Thanks for you reply.But resource are correct..
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,752
#4: Feb 5 '09

re: Supplied argument is not a valid MySQL result resource.


Hi.

Quote:

Originally Posted by kalyanip14 View Post

Thanks for you reply.But resource are correct..

Apparently, they are not.
PHP warnings do not lie, and this one is telling you that the resource is not valid.

You should always make sure of this yourself before you try to use the resource returned by a function.
The usual way to do that with MySQL queries is to do:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query("your query here") or die(mysql_error());
This prints the error you SQL is generating and stops the code from executing any further, if the SQL query fails.
If the SQL query is successful, it moves on the the next line of code.

If you want to handle this more gracefully, you can simply check the $result of the query before it is used. If it is FALSE, your query failed.
Newbie
 
Join Date: Feb 2009
Posts: 3
#5: Feb 9 '09

re: Supplied argument is not a valid MySQL result resource.


I sloved it thanks for your help
Reply