By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,491 Members | 3,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,491 IT Pros & Developers. It's quick & easy.

Supplied argument is not a valid MySQL result resource.

P: 3
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
Feb 5 '09 #1
Share this Question
Share on Google+
4 Replies


Dormilich
Expert Mod 5K+
P: 8,639
I guess the query failed and thus returned false (which is not a valid resource).
Feb 5 '09 #2

P: 3
Thanks for you reply.But resource are correct..
Feb 5 '09 #3

Atli
Expert 5K+
P: 5,058
Hi.

@kalyanip14
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.
Feb 5 '09 #4

P: 3
I sloved it thanks for your help
Feb 9 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Browse more PHP Questions on Bytes