简体   繁体   中英

What's wrong with my PDO statement?

// Check for existence - don't add a duplicate
$sqlQuery = $pdo->prepare('SELECT campaign_id FROM campaigns WHERE (customer_id=:customerId) AND (title=:campaignTitle) AND (description=:campaignDescription) AND (start_time=:startTimeStamp) AND (end_time=:endTimeStamp)');

$sqlQuery->bindParam(':customerId', $customerId);  // , PDO::PARAM_INT
$sqlQuery->bindParam(':campaignTitle', $campaignTitle);
$sqlQuery->bindParam(':campaignDescription', $campaignDescription);
$sqlQuery->bindParam(':startTimeStamp', $campaignTitle);
$sqlQuery->bindParam(':endTimeStamp', $endTimeStamp);

$sqlResult = DatabaseCommand($sqlQuery);

results in

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 ':customerId) AND (title=:campaignTitle) AND (description=:campaignDescription) A' at line 1' in E:\\coding\\Web Development\\Xampp\\htdocs\\api\\addCampaign.php:42 Stack trace: #0 E:\\coding\\Web Development\\Xampp\\htdocs\\api\\addCampaign.php(42): PDO->query('SELECT campaign...') #1 {main} thrown in E:\\coding\\Web Development\\Xampp\\htdocs\\api\\addCampaign.php on line 42

but I can't see why


[Update] for those who wanted to see the code of DatabaseCommand() this is pretty much it.

function DatabaseCommand($sqlCommand)
{
   $result = $sqlCommand->execute();
   return $result;
}

There is some additional code, but that just logs the command for debugging porpoises, checks for errors, logs those, catches exception & emails me.

update: seems like this isn't the solution, only improves readability

put a space between = and the parameter:

$sqlQuery = $pdo->prepare('SELECT campaign_id FROM campaigns WHERE (customer_id= :customerId) AND (title= :campaignTitle) AND (description= :campaignDescription) AND (start_time= :startTimeStamp) AND (end_time= :endTimeStamp)');

This code you posted here has nothing to do with error message you get.

You have to check addCampaign.php file, line 42 where you are using query() method instead of execute(). And of course you have to check the actual file that being executed.

I'll take the opportunity to direct all the enthusiast programmers' attention to the extreme helpfulness of reading error messages. Despite of the common belief, it is not just a reproach, reading "You've done something wrong!", leaving you to guess the reason, but precise and detailed explanation. And it only takes to read the error message to get the clue.

I'll also take the opportunity to direct all the enthusiast programmers' attention to the fact that if common practice of echoing only error message, leaving stack trace behind, were used, the information on the real cause of error were omitted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM