Jump to content

PDO Prepared Statement Syntax


Go to solution Solved by mac_gyver,

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/286822-pdo-prepared-statement-syntax/
Share on other sites

  • Solution

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`

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.