1.11M Members

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL

 
0
 

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!!!

 
0
 

thanks

 
0
 

still need help with my error though

 
0
 

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.

 
0
 

thanks for the reply. unfortunately im still getting the error

 
0
 

What is the error? Can you please post the newest version of the code?

 
0
 
<?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

 
0
 

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.

 
0
 

ok will try this

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

tht was displayed

 
0
 

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.

 
0
 

ok wil do

 
0
 

The error is no more but it prevents my autosuggest feature from working

 
0
 

As you can see there is no value for the Category in the query. Is this OK?

 
0
 

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)
 
0
 

Probably i should add a line to check for the category

 
0
 

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

 
0
 

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.

 
0
 
Array ( [search_term] => s ) 

That was displayed

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article