4 Replies - 667 Views - Last Post: 10 January 2013 - 04:36 PM Rate Topic: -----

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

mysql underscore escape not working?

Posted 09 January 2013 - 05:09 PM

$name='john_doe';
$name=str_replace('_','\_',$name);


$sql="select * from table where customer='".$name."'";


in the table, i have john_doe, john_smith, etc.

I am not getting the expected result?
Is This A Good Question/Topic? 0
  • +

Replies To: mysql underscore escape not working?

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3252
  • View blog
  • Posts: 10,903
  • Joined: 12-December 12

Re: mysql underscore escape not working?

Posted 09 January 2013 - 05:38 PM

You might use

$name=str_replace('_','\\_',$name);

although, is there really a need to escape underscores?
Was This Post Helpful? 1
  • +
  • -

#3 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: mysql underscore escape not working?

Posted 09 January 2013 - 05:47 PM

You don't need to escape underscores when doing an equality comparison. Underscores are a single-character wildcard for LIKE comparisons, but that doesn't apply to = comparisons - those are for exact matches, no wildcards supported. So for this particular query, there's no need to escape the underscore at all.
Was This Post Helpful? 2
  • +
  • -

#4 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: mysql underscore escape not working?

Posted 09 January 2013 - 08:14 PM

View PostAdaHacker, on 09 January 2013 - 05:47 PM, said:

You don't need to escape underscores when doing an equality comparison. Underscores are a single-character wildcard for LIKE comparisons, but that doesn't apply to = comparisons - those are for exact matches, no wildcards supported. So for this particular query, there's no need to escape the underscore at all.


really? it does not work for me at all without escapes.. thats why i use escape.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3252
  • View blog
  • Posts: 10,903
  • Joined: 12-December 12

Re: mysql underscore escape not working?

Posted 10 January 2013 - 04:36 PM

View Postg37752, on 09 January 2013 - 08:14 PM, said:

really? it does not work for me at all without escapes.. thats why i use escape.


Perhaps the data is being saved with a backslash?! Although, this would be a bit strange, as the backslash would have been treated as the escape-code(?).

How does it look in PHPMyAdmin? You can view the table(s) to look at the raw data.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1