Connecting Tech Pros Worldwide Help | Site Map

Get error while returning data from mysql

Familiar Sight
 
Join Date: Jan 2008
Posts: 198
#1: Sep 24 '09
When I retriwe data from mysql I got an error saying "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\xxxr\xxx.php on line 34". Please help me.
Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT VehicleMake,VehicleType FROM vehicles WHERE VehicleType=$type AND VehicleMake=$make";
  2.     echo $sql;
  3.     $result = mysql_query($sql);
  4.     while($row = mysql_fetch_array($result))
  5.     {
  6.     echo $row['VehicleMake'] . " " . $row['VehicleType'];
  7.     echo "<br />";
  8.     }
  9.  
  10.  
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,743
#2: Sep 24 '09

re: Get error while returning data from mysql


Hey.

If the mysql_query call fails, it will return FALSE instead of a MySQL resource.
Also, if the query doesn't return a result table (like with INSERT or UPDATE queries), the call will return TRUE, rather then a MySQL resource.

The mysql_fetch_array function only takes a MySQL resource object, so if you don't make sure the query result is a resource, you will get this warning.

Typically, you do something like:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query($sql);
  2. if($result) {
  3.   while($row = mysql_fetch_array($result)) {
  4.     // etc.
  5.   }
  6. }
Or just
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query($sql) or die(mysql_error());
  2. while($row = mysql_fetch_array($result)) {
  3.     // etc.
  4. }
Reply