0

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.

2 Answers 2

2

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`
Sign up to request clarification or add additional context in comments.

5 Comments

Well spotted. I would avoid calling columns reserved words altogether.
popnoodles how did you get the back ticks to show in a comment?
test comment `desc`
THANK YOU... haha didn't know desc was a protected word. It worked 100% fine now. You sir just saved me! Thanks a lot!
user is not a reserved word => dev.mysql.com/doc/refman/5.5/en/reserved-words.html - however there are a few words that do resemble (it), but is indeed not a reserved word; desc is. ;-) - It is good practice to use backticks for both, but not necessary for unreserved words.
2

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());
    } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.