McMaster Posted July 5, 2010 Share Posted July 5, 2010 Hey, I have prepared a search script so that it searches keywords in the database. For example, if I search, "Decline User" in the help search it shows all of the results but I am getting the following error: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/juiceoff/public_html/search_help.php on line 389 $var = addslashes($_POST['query']); $trimmed = trim($var); $trimmed_array = explode(" ",$trimmed); foreach ($trimmed_array as $trimm){ $query = "SELECT * FROM help_topics WHERE topic LIKE '%$trimm%' OR text like '%$trimm%'"; $numresults = mysql_query ($query); $row_num_links_main = mysql_num_rows($numresults); $numresults = mysql_query ($query); $row= mysql_fetch_array ($numresults); do { $adid_array[] = $row['topic']; } while( $row= mysql_fetch_array($numresults)); } $tmparr = array_unique($adid_array); $i=0; foreach ($tmparr as $v) { $newarr[$i] = $v; $i++; } $j=0; foreach($newarr as $value){ $query_value = "SELECT * FROM help_topics WHERE topic = '$value' OR text = '$value'"; $num_value = mysql_query ($query_value); //$row_linkcat = mysql_fetch_array ($num_value); //$row_num_links= mysql_num_rows ($num_value); $row = mysql_fetch_array($num_value, MYSQL_ASSOC); ?> <ul style="margin-bottom: 0pt;"> <?php echo "<li><a href=\"http://www.website.com/help_topic.php?id=".$row['id']."\">".$value."</a></li>"; ?> </ul> } The error is coming from this line: $row = mysql_fetch_array($num_value, MYSQL_ASSOC); What can I do to remove this? Quote Link to comment https://forums.phpfreaks.com/topic/206796-search-script-working-but-mysql_fetch_array-error/ Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 supplied argument is not a valid MySQL result resource Quote Link to comment https://forums.phpfreaks.com/topic/206796-search-script-working-but-mysql_fetch_array-error/#findComment-1081506 Share on other sites More sharing options...
McMaster Posted July 5, 2010 Author Share Posted July 5, 2010 Tried mysql_fetch_assoc also and was resulted in a similar error. What do I need to do to get rid of this problem as it's doing my head in lol. Quote Link to comment https://forums.phpfreaks.com/topic/206796-search-script-working-but-mysql_fetch_array-error/#findComment-1081510 Share on other sites More sharing options...
kenrbnsn Posted July 5, 2010 Share Posted July 5, 2010 That error says that you have an error with your query, change: <?php $query = "SELECT * FROM help_topics WHERE topic LIKE '%$trimm%' OR text like '%$trimm%'"; $numresults = mysql_query ($query); $row_num_links_main = mysql_num_rows($numresults); $numresults = mysql_query ($query); $row= mysql_fetch_array ($numresults); ?> to <?php $query = "SELECT * FROM help_topics WHERE topic LIKE '%$trimm%' OR text like '%$trimm%'"; $numresults = mysql_query ($query) or die("Problem with the query: $query<br>" . mysql_error()); $row_num_links_main = mysql_num_rows($numresults); $row= mysql_fetch_array ($numresults); ?> And report what prints. Ken Quote Link to comment https://forums.phpfreaks.com/topic/206796-search-script-working-but-mysql_fetch_array-error/#findComment-1081513 Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 Tried mysql_fetch_assoc also and was resulted in a similar error. What do I need to do to get rid of this problem as it's doing my head in lol. It has nothing to do with that, The argument given within the mysql_fetch_array(); is the problem.. Meaning your problem lies within this line of code $query_value = "SELECT * FROM help_topics WHERE topic = '$value' OR text = '$value'";$num_value = mysql_query ($query_value); Try printing the error by doing this $query_value = "SELECT * FROM help_topics WHERE topic = '$value' OR text = '$value'";$num_value = mysql_query ($query_value) or die(mysql_error()); And give us back the output. Quote Link to comment https://forums.phpfreaks.com/topic/206796-search-script-working-but-mysql_fetch_array-error/#findComment-1081514 Share on other sites More sharing options...
McMaster Posted July 5, 2010 Author Share Posted July 5, 2010 Okay, I done both of those things and funny enough, no results show what so ever except from one empty list item. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/206796-search-script-working-but-mysql_fetch_array-error/#findComment-1081525 Share on other sites More sharing options...
jcbones Posted July 5, 2010 Share Posted July 5, 2010 There is alot of redundancy in that code. Over 1/3 of it can be cut out. Unless I missed something. This should return what you want. <?php $var = addslashes($_POST['query']); $trimmed = trim($var); $trimmed_array = explode(" ",$trimmed); foreach ($trimmed_array as $trimm){ $query = "SELECT * FROM help_topics WHERE topic LIKE '%$trimm%' OR text like '%$trimm%'"; $numresults = mysql_query ($query); $row_num_links_main = mysql_num_rows($numresults); if($row_num_links_main > 0) { while( $row= mysql_fetch_array($numresults)){ <ul style="margin-bottom: 0pt;"> <?php echo "<li><a href=\"http://www.website.com/help_topic.php?id=".$row['id']."\">".$row['topic'] ."</a></li>"; ?> </ul> <?php } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/206796-search-script-working-but-mysql_fetch_array-error/#findComment-1081534 Share on other sites More sharing options...
McMaster Posted July 5, 2010 Author Share Posted July 5, 2010 Ah wait I got it. It's because I forgot to add slashes to the variables that were being queried. Thanks for your help guys. Quote Link to comment https://forums.phpfreaks.com/topic/206796-search-script-working-but-mysql_fetch_array-error/#findComment-1081538 Share on other sites More sharing options...
phpSensei Posted July 5, 2010 Share Posted July 5, 2010 Ah wait I got it. It's because I forgot to add slashes to the variables that were being queried. Thanks for your help guys. Yeah, everytime you get that error always trace if back to the root of where the problem is. In this case using mysql_error() the error is ouputted to the screen, therefor easier to fix the problem then just trying to guess it out... Mark this thread as solved please Quote Link to comment https://forums.phpfreaks.com/topic/206796-search-script-working-but-mysql_fetch_array-error/#findComment-1081542 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.