Jump to content

mysql_real_escape_string() small problem


robert_gsfame

Recommended Posts

How are  you trying to use LIKE?

 

Do you have "apple" and you want to match "apple" | "apples"? 

 

When you put LIKE '% %" you are going to match anything with a space in it.

 

When you match LIKE '%s' you will match anything ending in s.

 

What is it you want to match?

Link to comment
Share on other sites

okay...this problem solve but what if i create a dynamic query for example

 

$a=$_GET['a'];

if(!empty($a)){

$a="AND name LIKE'%".$a."%'";}

 

and i wish this query to be execute using mysql_real_escape_string()

SELECT * FROM table1 WHERE column1='record1' $a

 

so that when $a not empty i will have this query

SELECT * FROM table1 WHERE column1='record1' AND name LIKE '%a%'

 

else

 

SELECT * FROM table1 WHERE column1='record1'

 

 

Link to comment
Share on other sites

$a=stripslashes(mysql_real_escape_string($_GET['a']));                    //original example says a="apples"
$sqlLike=is_Null($a) ? " AND name LIKE '%$a%' : "";

$sql="SELECT * FROM table1 WHERE column1 = 'record1'".$sqlLike;

 

should set sql=

SELECT * FROM table1 WHERE column1 = 'record1' AND name LIKE '%apples%'

 

I believe if you mysql_real_escape it surrounds the value with ' and you need to stripslashes on it to get rid of them.  Depends on your installation though, safer to use it in this example.

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.