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

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());

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 "
";
}
}

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>";
}
}

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 " "; 
} 

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()); 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.