0

Today I got an unusual response when trying to make a few queries, here is the error output.

[17-Feb-2014 12:37:24 America/Denver] PHP Warning:  PDOStatement::execute():
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 'key = 'AH3D'' at line 1 in file on line 28

Here is the code I was using, this is how i've always done it.

public function get($key = null) {
    $get = $this->conn->prepare("SELECT url FROM urls WHERE key = :get");
    $get->execute(array(':get' => $key));
    return $get->fetch();
}

How I call the function.

echo $tiny->get($_GET['key']);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
ykykykykyk
  • 446
  • 1
  • 3
  • 10
  • possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – Ian Ringrose May 06 '14 at 10:13

1 Answers1

1

Key is a mysql reserved keyword you need to use back-ticks arround your columns name key

$get = $this->conn->prepare("SELECT url FROM urls WHERE `key` = :get");

Mysql Reserved Words

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
  • 1
    Thanks a lot, I used to put back-ticks but then stopped because I thought they did nothing. I'll have to start doing it again and read up on key terms. – ykykykykyk Feb 17 '14 at 19:49
  • A good practice is to use the back-ticks it saves time for this type of errors and also if you find this answer as helpfull then read [Accepting Answers: How does it work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) – M Khalid Junaid Feb 17 '14 at 20:56
  • 1
    @downvoters atleast show some respect and provide your feedback about how to make the answer more good – M Khalid Junaid Feb 17 '14 at 20:59