RealDope Posted March 9, 2014 Share Posted March 9, 2014 New to PHP, some background with SQL. Have no idea what is wrong with my SQL syntax, can't for the life of me figure it out. try { $stmt = $pdo->prepare('INSERT INTO ' . $keytable . ' (key, username, expiration) VALUES (?, ?, ?)'); } catch(PDOException $ex) { echo json_encode(array( "result" => false, "error" => "SQL prepare failed" )); die(); } try { echo $stmt->queryString; $stmt->bindParam(1, $apikey); $stmt->bindParam(2, $input_username); $stmt->bindParam(3, $expiration); $stmt->execute(); } catch(PDOExceptoin $ex) { echo json_encode(array( "result" => false, "error" => "Insertion execution failed" )); die(); } When this is run, I get the following 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 'key, username, expiration) VALUES ('6dbd32cfded2383ea526250c08e00f25da522f11ccf4' at line 1' in /home1/realdope/public_html/beta/login.php:95 Stack trace: #0 /home1/realdope/public_html/beta/login.php(95): PDOStatement->execute() #1 {main} thrown in /home1/realdope/public_html/beta/login.php on line 95 I know Java, and looked up the PHP Syntax for catch statements. Did I do it wrong? I thought I had the catch setup properly, but the error says the exception is uncaught. Additionally, what's wrong with my SQL syntax? That exact query with different column names works in another file. Quote Link to comment https://forums.phpfreaks.com/topic/286822-pdo-prepared-statement-syntax/ Share on other sites More sharing options...
Solution mac_gyver Posted March 9, 2014 Solution Share Posted March 9, 2014 the Uncaught exception is because you have spelling error in -- catch(PDOExceptoin $ex) the error in the query is because key is a reserved mysql keyword. either rename the column to something else or enclose key in back-ticks `key` Quote Link to comment https://forums.phpfreaks.com/topic/286822-pdo-prepared-statement-syntax/#findComment-1471887 Share on other sites More sharing options...
RealDope Posted March 9, 2014 Author Share Posted March 9, 2014 Thanks a ton dude. Quote Link to comment https://forums.phpfreaks.com/topic/286822-pdo-prepared-statement-syntax/#findComment-1471888 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.