error: Warning: mysql_fetch_array(): supplied argument is no

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
buddyt
Forum Newbie
Posts: 2
Joined: Wed Jan 18, 2012 5:49 pm

error: Warning: mysql_fetch_array(): supplied argument is no

Post by buddyt »

I am getting the following error when I run the code below. Please advise if you can tell me why. Thank you in advance.

Code: Select all

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource[ /code ]

Here's the code that's generating the error: 
[syntax="php" ]
<?php
$con = mysql_connect("*********","**********","**********");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  } 

$result = mysql_query("SELECT * FROM Users");
echo "<br />";
echo $result;

while($row = mysql_fetch_array($result))
  {
  echo $row['First_Name'] . " " . $row['Last_Name'];
  echo "<br />";
  }

mysql_close($con);

?>
[ /syntax]
JohnK_UK
Forum Newbie
Posts: 5
Joined: Wed Jan 11, 2012 12:16 pm

Re: error: Warning: mysql_fetch_array(): supplied argument i

Post by JohnK_UK »

As the error message says, $result is not a valid resource, so your query must have failed. There's nothing wrong with the syntax, so maybe you don't have permission to run select queries, or maybe the Users table doesn't exist. (Is it perhaps called users?) In any case, inserting

Code: Select all

if (!$result) {
    die('Invalid query: ' . mysql_error());
}
after the query should point you in the right direction.

FWIW, it's good practice ALWAYS to check the validity of a resource before trying to use it.
buddyt
Forum Newbie
Posts: 2
Joined: Wed Jan 18, 2012 5:49 pm

Re: error: Warning: mysql_fetch_array(): supplied argument i

Post by buddyt »

John, Thanks for your post. What seemed to be the problem was that I was missing "mysql_select_db("buddytip", $con);" because when I added that the problem went away.
Post Reply