We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,613 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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

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

4
Contributors
51
Replies
6 Days
Discussion Span
3 Months Ago
Last Updated
53
Views
Question
Answered
DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

just a headsup, they are removing this function

http://php.net/manual/en/function.mysql-fetch-assoc.php

bradly.spicer
Junior Poster
117 posts since Oct 2012
Reputation Points: 0
Solved Threads: 7
Skill Endorsements: 0

thanks

DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

still need help with my error though

DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

thanks for the reply. unfortunately im still getting the error

DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
<?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

DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

ok will try this

DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
SELECT `Category`, `Type` FROM `autosuggest` WHERE `Category`=''` AND `Type` LIKE 's%

tht was displayed

DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

ok wil do

DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

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)
broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13

Probably i should add a line to check for the category

DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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

DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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.

broj1
Nearly a Posting Virtuoso
1,211 posts since Jan 2011
Reputation Points: 167
Solved Threads: 164
Skill Endorsements: 13
Array ( [search_term] => s ) 

That was displayed

DamzWildfire
Junior Poster in Training
51 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3582 seconds using 2.79MB