Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL Syntax Error - SELECT statement [duplicate]

Tags:

php

mysql

I'm getting this error displayed on my screen I have been trying to debug.

"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 = 'testname'' at line 1"

my function im using for this is as follows:

function recentMessages() {
    $tbl_name="messages";
    $username = $_SESSION['username'];
    $result = mysql_query("SELECT * FROM $tbl_name WHERE to = '$username' ") or die(mysql_error());
    while ($row = mysql_fetch_row($result))
      {
        return $row['date']." ".$row['time']." ".$row['from']." ".$row['subject']. "<br />";    
      }
}

Basically what im trying to do is to get all the rows of data from the database messages where who its 'to' is the username of the session and its echo'd out. Any ideas on what im doing wrong? thanks

like image 596
Nexus9988 Avatar asked Apr 10 '26 13:04

Nexus9988


1 Answers

to is a reserved word. Encase it in tick marks.

... WHERE `to` = '$username'

See the MySQL reserved words.

You should avoid using reserved words if possible.

like image 137
Kermit Avatar answered Apr 12 '26 05:04

Kermit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!