1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

SQL Syntax Error

Discussion in 'PHP' started by jonathandey, Oct 6, 2010.

  1. #1
    I'm getting an error here somewhere
        $username = $_SESSION['username'];
        
        //Get Mail
        $mail = mysql_query("SELECT * FROM messages WHERE to = '".$username."'") or die(mysql_error());
        $countMail = mysql_num_rows($mail);
    
    PHP:
    However if I change the text messages to users and "to" to username it'll work fine

    Thanks
     
    jonathandey, Oct 6, 2010 IP
  2. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #2
    The error message I'am getting is
    
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to='Admin'' at line 1
    
    Code (markup):
     
    jonathandey, Oct 6, 2010 IP
  3. clockedout7

    clockedout7 Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    'to' is a reserved word in SQL. Surround it with quotes in your query and the error should be gone.
     
    clockedout7, Oct 6, 2010 IP
  4. jonathandey

    jonathandey Active Member

    Messages:
    112
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    That worked! thanks for the help! :)
     
    jonathandey, Oct 6, 2010 IP
  5. ilyakar

    ilyakar Peon

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    @clockedout7, thanks you helped me out too, i had similar problem :)
     
    ilyakar, Oct 7, 2010 IP
  6. Rajeev123

    Rajeev123 Peon

    Messages:
    100
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i am agree with this answer
     
    Rajeev123, Oct 20, 2010 IP
  7. Tastyweb

    Tastyweb Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    As a general rule, I always wrap my field names in ` characters. eg, `field_name` - that way you never get that kind of issue, and I find it helps me read my mySQL syntax a bit better.
     
    Tastyweb, Oct 21, 2010 IP