Easy Syntax fix for LIKE query with variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Adam.A
Forum Newbie
Posts: 1
Joined: Wed Mar 11, 2009 11:16 pm

Easy Syntax fix for LIKE query with variable

Post 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
vmene
Forum Newbie
Posts: 3
Joined: Wed Apr 16, 2008 3:59 am

Re: Easy Syntax fix for LIKE query with variable

Post 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.
Post Reply