php - What's wrong with my PDO statement?

872
// 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 ofDatabaseCommand() 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.

540

Answer

Solution:

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)');
528

Answer

Solution:

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.

People are also looking for solutions to the problem: Server settings for posting checkbox & nesting values in PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.