Why doesn't this query work?

Discussion in 'MySQL' started by Tony Brar, Jul 28, 2013.

  1. #1
    Hi DP,

    On my website I have a SQL query that seems to be causing an error:​
    SELECT content_url,icon_url,content_type,title,shortdesc,desc,rating,date,views,downloadable FROM content WHERE content_id=1
    Code (markup):
    It causes this error:​
    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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,rating,date,views,downloadable FROM content WHERE content_id=1' at line 1' in /home/content/42/9125042/html/contentpage.php:20 Stack trace: #0 /home/content/42/9125042/html/contentpage.php(20): PDO->query('SELECT content_...') #1 {main} thrown in /home/content/42/9125042/html/contentpage.php on line 20
    Here is the code around it (PHP):​
    [​IMG]
    What's causing the error in my code?​
    Thanks,​
    -Tony​

     
    Tony Brar, Jul 28, 2013 IP
  2. GORF

    GORF Well-Known Member

    Messages:
    224
    Likes Received:
    21
    Best Answers:
    3
    Trophy Points:
    165
    #2
    Is there actually an entry that has content_id of 1?

    Try spaces ... WHERE content_id = 1
    Try quotes, single-quotes or bacticks ... WHERE content_id = '1'

    If you are selecting most or all of the fields from the table, use * for all:
    SELECT * FROM content WHERE content_id = 1
     
    GORF, Jul 28, 2013 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,830
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #3
    $sql = SELECT `content_url`, `icon_url`, `content_type`, `title`, `shortdesc`, `desc`, `rating`, `date`, `views`,`downloadable`
    FROM `content`
    WHERE `content_id`='1'";
    Code (markup):
    I've shown how it looks with the backticks - in particular I'd be concerned about the column `desc`, there's no need to have shortened column names - I'd recommend renaming it to description.

    The next step after making those changes, if the problem persists, is to cut and paste the query into phpMyAdmin and see if you get a more useful error message.
     
    sarahk, Jul 28, 2013 IP