Jump to content

searching/echoing any unknown single or multiple word search


paulmo

Recommended Posts

error:

mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource 

 

$message_string =$_POST['message'];
$pre_filter=trim($message_string);
$get_search=explode(" ",$pre_filter);
$sql = "SELECT * FROM beta";
foreach ($get_search as $final_string){
  $sql .= "WHERE terms LIKE '%final_string%' or";
}
$sql = substr_replace($sql,"",-2); //take off the last 'or'
$posts =mysql_query($sql);
//}
$n=0;
while($row=mysql_fetch_assoc($posts)){
$n++;
echo $row['terms'];
echo "<br>";
}

 

i'm not looking to match my own LIKE words, but any user submitted word/words (or echo none if none found), matched against db table field named 'terms.' thanks for help.

mysql 4.1

Link to comment
Share on other sites

You're concatenating multiple WHERE clauses onto $sql in your foreach loop, and essentially giving mysql_query an invalid statement.

 

Put this in as well:

 

$posts =mysql_query($sql) or die(mysql_error());

Link to comment
Share on other sites

Try something like this:

 

$message_string =$_POST['message'];
$pre_filter=trim($message_string);
$get_search=explode(" ",$pre_filter);
foreach ($get_search as $final_string){
$sql = "SELECT * FROM beta WHERE terms LIKE '%{$final_string}%'";
$sql = substr_replace($sql,"",-2); //take off the last 'or'
$posts =mysql_query($sql) or die(mysql_error());
$n=0;
while($row=mysql_fetch_assoc($posts)){
$n++;
echo $row['terms'];
echo "
";
}
}

Link to comment
Share on other sites

did your edits; getting error message below:

You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'WHERE terms LIKE '%
final_string%' orWHERE terms LIKE '%final_string%" at line 1

 

the line in question is line 140, not 1. the quotes are ' ' around final_string. i've checked a text and manual; the syntax seems right. any ideas? thanks

$message_string =$_POST['message'];
$pre_filter=trim($message_string);
$get_search=explode(" ",$pre_filter);
$sql = "SELECT * FROM beta";
foreach ($get_search as $final_string)
{
$sql = "WHERE terms LIKE '%final_string%' or";
$sql .= substr_replace($sql,"",-2); //take off the last 'or'
$posts = mysql_query($sql) or die (mysql_error ());
$n=0;
while($row=mysql_fetch_assoc($posts))
{
$n++;
echo $row['terms'];
echo "<br>";
}
}

Link to comment
Share on other sites

Please echo $sql and post it here.

Resource id #2

query as of now:

$message = strtoupper($message); 
$message = strip_tags($message); 
$message = trim ($message); 
$message = str_split($message); 
$name = trim ($name); 
$name = strip_tags($name); 
$name = mysql_real_escape_string($name); 

//$message = preg_split("/[\s,]+/")($message);

$data = mysql_query("SELECT * FROM xxx WHERE terms LIKE'%$message%'"); 
echo $data; 

while($result = mysql_fetch_array($data)) 
{ 
echo $result['terms']; 
echo " "; 
} 

Link to comment
Share on other sites

Instead of:

 

$data = mysql_query("SELECT * FROM xxx WHERE terms LIKE'%$message%'");
echo $data; 

 

do this:

 

$sql = "SELECT * FROM xxx WHERE terms LIKE '%$message%'";
echo $sql;
mysql_query($sql) or die(mysql_error()); 

Link to comment
Share on other sites

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.