Page 1 of 1

Easy Syntax fix for LIKE query with variable

Posted: Wed Mar 11, 2009 11:27 pm
by Adam.A
Hi All,

I am trying to create a simple word filter for my database and I am having trouble with a little bit of syntax that passes the variable into the LIKE query.

I have had a good look through some forums and found this solution but I can’t get it to work with my code.(http://www.phpfreaks.com/forums/index.p ... 601.0.html)

I think I am really close and it’s just a issue with the quotation marks.

Code: Select all

$badwords=array("ass","babbi","penis");
foreach ($badwords as $badword)
{      
    //Delete query
    
$delete_sql = "DELETE FROM jos_facileforms_subrecords WHERE name LIKE \'%'.$badword.'%\' "; 
 
 
    // execute $delete_sql  query and get result  
 
    $filter_sql_result = mysql_query($delete_sql,$connection)  
        or die(mysql_error());   
echo $filter_sql_result; 
 }      
any assistance would be greatly appreciated i have been searching for a solution for quite some time now.

Cheers,
Adam

Re: Easy Syntax fix for LIKE query with variable

Posted: Thu Mar 12, 2009 12:13 am
by vmene
"DELETE FROM jos_facileforms_subrecords WHERE name LIKE '%'.$badword.'%' ";

change your query to

"DELETE FROM jos_facileforms_subrecords WHERE name LIKE '%".$badword."%' ";

it will work.