Jump to content

Recommended Posts

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?

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

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.

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.