Querry syntax error on like '%$variable'

Discussion in 'Databases' started by nebhotep, May 22, 2008.

  1. #1
    Hi

    if i have my select like this :

    SELECT * FROM Table where Field like '%MyName%'

    it works,
    but when i try to put the string 'MyName' in a constant it does not work

    $String = 'MyName';

    SELECT * FROM Table where Field like '\%{$String}\%'


    how shiuld I use % with string?
     
    nebhotep, May 22, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    If you escape your '%', it is not looking for the pattern contained in $String, it would be looking for \%MyName\%. Get rid of the slashes and it should work.
     
    jestep, May 22, 2008 IP
  3. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #3
    if i make it like this

    SELECT * FROM Table where Field like '%{$String}%'

    still not works
     
    nebhotep, May 22, 2008 IP
  4. bluecape

    bluecape Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try this

    SELECT * FROM Table where Field like '%".$String."%'
    Code (markup):
     
    bluecape, May 22, 2008 IP
  5. nebhotep

    nebhotep Active Member

    Messages:
    244
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #5
    thanks
    it works
     
    nebhotep, May 22, 2008 IP
  6. bluecape

    bluecape Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    you're welcome :)
     
    bluecape, May 22, 2008 IP