Take the 2-minute tour ×

The database connect is all correct, but I am getting an error saying

mysql_fetch_array(): supplied argument is not a valid MySQL result on line 31

  $hostname = "localhost";  
  $username = "user";  
  $password = "pass";   
  $usertable = "products";  
  $dbName = "test_prods";  

 $s = $_GET["s"];  
 $name = $row["product_name"];  

     MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");   
     @mysql_select_db( "$dbName") or die( "Unable to select database");   

 //error message (not found message)   
   $XX = "No Products Found";   

 $query = mysql_query("SELECT * FROM $usertable WHERE $s LIKE '$name'");   
  while ($row = mysql_fetch_array($query))   
print ("this is for $variable1, and this print the $variable2 end so on...");  

 //below this is the function for no record!!   
if (!$variable1)  
       print ("$XX");  
share|improve this question

closed as too localized by casperOne Jan 2 '13 at 13:01


You could check mysql_error after calling mysql_query to see if there were any problems running your query. –  Zach Rattner Jan 2 '13 at 0:33
$name = $row["product_name"]; --- please explain this line –  zerkms Jan 2 '13 at 0:33
Please not that the old mysql_% functions will be discontinued. That means that your code can stop working when PHP is upgraded. For new projects you should switch to the mysqli extenstion as recommended in the PHP docs. –  Jasny - Arnold Daniels Jan 2 '13 at 0:34
@Arnold Daniels: there is advice to choose between mysqli/pdo, not a straight "switch to mysqli" –  zerkms Jan 2 '13 at 0:35
@zerkms, you're right! The API of PDO is very different from the old mysql. So if someone is already familiar with that I usually recommend swithing to mysqli rather than PDO. –  Jasny - Arnold Daniels Jan 2 '13 at 0:38

3 Answers 3


$s = $_GET["s"];  
$name = $row["product_name"];  


$s = mysql_real_escape_string($_GET["s"]);
$name = mysql_real_escape_string($row["product_name"]);

Be warned heed this warning: *This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.*


AND Change your query to:

      or die(mysql_error());

Will tell you if there is a error in your SQL that will narrow down the problem! If you can tell me what result you get i can update the answer more suited to the issue.

share|improve this answer

There is probably an error in you sql syntax.

On this line here

$name = $row["product_name"];  

does $row["product_name"] evaluate to anything ?

From what you've posted it doesn't, but maybe there is a value loaded in elsewhere.

if it doesn't evaluate to anything then the sql statement will have an error in syntax and $query can not become a mysql resource.

share|improve this answer

You seem to be using an undefined variable:

$name = $row["product_name"];

I guess you are trying to fetch the name from another query and then use it to fetch your matches if any.

Setting the name before calling mysql_query will most likely do the job for you.

Also, from the looks of it you are just starting with mysql, so let me suggest that you give up on learning to use the mysql_ functions, and start learning how to run your queries using the PDO.

I've found this tutorial quite helpful.

share|improve this answer
