franpen
franpen

Reputation: 322

SQL query in a PHP based website

I have a table for files with 3 rows: name, desc, user.

Name is the name of the file, for example: cert1.pdf Desc is a description of the file, for example: First exam of the year. User is the name of the user who uploaded the file to the server, for example: John Doe.

Now I want to retrieve the name and the description of cert1.pdf. So this is what I got:

$sql = "SELECT desc, user
    FROM files AS f
    WHERE f.name = 'cert1.pdf'";

    $result = mysql_query($sql);
    if (!$result) {
        die('Error: ' . mysql_error());
    } 

It must be something really stupid, but I keep having this error message:

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 'desc, user FROM files AS f WHERE f.name = 'cert1.pdf'' at line 1

Any idea of what am I doing wrong here? Thanks in advance.

Upvotes: 0

Views: 48

Answers (2)

Tin Tran
Tin Tran

Reputation: 6202

desc is a reserved words try putting desc inside back ticks (`) the same key as the ~ instead of desc user might be a reserved word too

try

SELECT `desc`,`user`

Upvotes: 2

Chris Rasco
Chris Rasco

Reputation: 2731

To show an example of Tin Tran's answer of escaping "desc" with backticks.

$sql = "SELECT `desc`, `user`
    FROM files AS f
    WHERE f.name = 'cert1.pdf'";

    $result = mysql_query($sql);
    if (!$result) {
        die('Error: ' . mysql_error());
    } 

Upvotes: 2

Related Questions