What's wrong with this code?

Discussion in 'PHP' started by mikebrad0927, Aug 25, 2007.

  1. #1
    $result = mysql_query('SELECT DISTINCT sec from tags WHERE main = $r ORDER BY sec');
    Code (markup):
    It's not returning a result. $r is set.
     
    mikebrad0927, Aug 25, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Use double quotes instead of single quotes. Variables are not parsed between single quotes.
     
    nico_swd, Aug 25, 2007 IP
  3. Ervee

    Ervee Peon

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3

    Take variables out of the quotes and use double quotes (and single quotes for strings). If r is an integer/float etc:

    $result = mysql_query("SELECT DISTINCT sec from tags WHERE main = ".$r." ORDER BY sec");

    if it's a string/date etc:

    $result = mysql_query("SELECT DISTINCT sec from tags WHERE main = '".$r."' ORDER BY sec");

    if $r is a set (like (1,2,3,4,5))

    $result = mysql_query("SELECT DISTINCT sec from tags WHERE main IN ".$r." ORDER BY sec");
     
    Ervee, Aug 25, 2007 IP
  4. mikebrad0927

    mikebrad0927 Peon

    Messages:
    266
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    yep, that was it. Changed the code to

    $result = mysql_query("SELECT DISTINCT sec from tags WHERE main = '".$r."' ORDER BY sec");
    Code (markup):
    and it worked.

    Thanks nico
     
    mikebrad0927, Aug 25, 2007 IP
  5. mikebrad0927

    mikebrad0927 Peon

    Messages:
    266
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    nice, ervee. I should have waited 2 seconds
     
    mikebrad0927, Aug 25, 2007 IP