whart is wrong with this code??? please check

Discussion in 'PHP' started by smart, Jan 26, 2008.

  1. #1
    Hi Guys,
    I am breaking my head for this error for an hour. Could you help me soon??

    The code below is not retriving data from Database.

    $course_id = CS100;
    $query = 'SELECT * FROM comments WHERE CourseID = "$course_id" ';

    But when i try this
    $query = 'SELECT * FROM comments WHERE CourseID = "CS100" ';

    It is working......

    Reply me soon please
     
    smart, Jan 26, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Variables are not parsed between single quotes. Do it like this:

    
    $course_id = 'CS100';
    $query = "SELECT * FROM comments WHERE CourseID = '$course_id'";
    
    PHP:
     
    nico_swd, Jan 26, 2008 IP
  3. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #3
    The above is Giving this Syntax Error
    Parse error: syntax error, unexpected T_STRING
     
    smart, Jan 26, 2008 IP
  4. babyboy808

    babyboy808 Well-Known Member

    Messages:
    491
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    125
    #4
    Try this:

    [COLOR=#0000ff]$course_id[/COLOR] = [COLOR=#ff0000]'CS100'[/COLOR];
    [COLOR=#0000ff]$query[/COLOR] = [COLOR=#ff0000]"SELECT * FROM comments WHERE CourseID = $course_id"[/COLOR];
    Code (markup):
     
    babyboy808, Jan 26, 2008 IP
  5. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #5
    It is Working as nico_swd said

    Thank you very much

    Hi babyboy808
    I tried that also but was giving the same Error.

    Anyhow it is corrected. Thank you
     
    smart, Jan 26, 2008 IP
  6. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    just to clarify things up, I think the most right / wise way to do the query is like this..for me, it's more visible to human eyes..

    $course_id = CS100;
    $query = "SELECT * FROM comments WHERE CourseID = '".$course_id."' ";
    PHP:
     
    fairuz.ismail, Jan 26, 2008 IP