Jump to content

PDO Prepared Statement Syntax


RealDope

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.