Start New Discussion within our Web Development Community

I was trying to loop through my database for some results and recieved this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /hermes/waloraweb032/b867/moo.classiadsinfo/ajax/searchApp.php on line 8

and heres my code:

<?php
require_once '../connect.php';

if(isset($_POST['search_term'])==true && empty($_POST['search_term'])==false)
{
$search_term=mysql_real_escape_string($_POST['search_term']);
$query=mysql_query("SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category=.'$_POST[category]'` && `Type` LIKE '$search_term%'");
while(($row=mysql_fetch_assoc($query))!==false)
{
  echo '<li><b>',$row['Type'],'</b></li>';
}

}
?> 

please help!!!

The $query variable does not contain a valid resource since there is an error in the query. You can not perform a concatenation within a double quoted string. Correct the query this way: remove the . and enclose the array element $_POST[category] in curly braces). I also changed && to AND since I am not sure whether && is valid in mysql. Also a backtick was missing after the Category.

$query=mysql_query("SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`='{$_POST[category]}'` AND `Type` LIKE '$search_term%'");

The name of the $query variable is missleading since it contains a resource not a query. I would change it to $resource.

$resource=mysql_query("SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`='{$_POST[category]}'` AND `Type` LIKE '$search_term%'");

while($row=mysql_fetch_assoc($resource))
{
    echo '<li><b>',$row['Type'],'</b></li>';
}

And as bradly.spicer said switch to newer mysqli instead of older mysql, when possible.

<?php
require_once '../connect.php';

if(isset($_POST['search_term'])==true && empty($_POST['search_term'])==false)
{
$search_term=mysql_real_escape_string($_POST['search_term']);
 $resource=mysql_query("SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`='{$_POST[category]}'` AND `Type` LIKE '$search_term%'");

while(($row=mysql_fetch_assoc($resource))!==false)
{
echo '<li><b>',$row['Type'],'</b></li>';
}

}
?> 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /hermes/waloraweb032/b867/moo.classiadsinfo/ajax/searchApp.php on line 11

Looks like there is still an error in the query. Can you add this line of code just after line 6:

    die("SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`='{$_POST[category]}'` AND `Type` LIKE '$search_term%'");

As you see it will display the constructed query. Can you please post it here.

You can also copy the displayed query and test it in phpmyadmin.

SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`=''` AND `Type` LIKE 's%

tht was displayed

There is a backtick just before the AND that should not be there. Remove it (and comment out the die statement) then try it out.

Yo check for search term before issuing the query but you do not check for Category. Is this OK? Is category not mandatory to have a value?

if(isset($_POST['search_term'])==true && empty($_POST['search_term'])==false)

how this should work is i have a drop down with different categories onece a category is select theres a type text box with an autosuggest feature attached. so when a category is seletect only the keywords associated with that category should be shown when typed

But category is obviously not in the $_POST. Can you put this line in the very beginning of the script and post the result:

die(print_r($_POST, 1));

This will print the contents of the $_POST array and stop the script.

This question has already been answered. Start a new discussion instead.