I am running into a weird issue with PDO. Sometimes it just doesn't execute the SQL. But it acts like it does.
$SQL = 'INSERT INTO
beam_stats (beam_id, beam_stat_type, beam_stat_count)
VALUES
(:BID, :BST, :BSC)
ON DUPLICATE KEY UPDATE
beam_stat_count = beam_stat_count + VALUES(beam_stat_count)';
$parameters = Array(':BID' => $beamID, ':BST' => $valueName, ':BSC' => $value);
$this->dbCon->prepare_execute($SQL, $parameters);
/**
* Prepares and Executes the provided statement and parameters.
*
* @param string $statement
* @param ARRAY $parameters
* @return mixed database results
*/
public function prepare_execute($statement, $parameters) {
$this->prepare($statement);
return $this->execute($parameters);
}
/**
* Prepares the provided database statement
*
* @author Jonathan Pitcher
*
* @param string $statement
*/
public function prepare($statement) {
$this->sth = $this->db->prepare($statement);
if (!$this->sth) {
error_log(print_r($this->db->errorInfo(), true));
}
}
/**
* Executes the prepared statement in sth using the passed parameters.
*
* @author Jonathan Pitcher
*
* @param ARRAY $parameters
* @return mixed
*/
public function execute($parameters) {
$results = $this->sth->execute($parameters);
$count = $this->sth->rowCount();
error_log($this->sth->queryString);
error_log("Updated $count rows.\n");
if (!$results) {
error_log(print_r($this->sth->errorInfo(), true));
return false;
}
return $results;
}
I log everything. So when it executes the logs print out what happens and how many rows got updated.
[Thu Jun 05 10:15:40 2014] [error] [client ] Updating Beam Stat 494 sold_out 1
[Thu Jun 05 10:15:40 2014] [error] [client ] INSERT INTO\n\t\t\t\t\t\tbeam_stats (beam_id, beam_stat_type, beam_stat_count)\n\t\t\t\t\tVALUES\n\t\t\t\t\t\t(:BID, :BST, :BSC)\n\t\t\t\t\tON DUPLICATE KEY UPDATE\n\t\t\t\t\t\tbeam_stat_count = beam_stat_count + VALUES(beam_stat_count)
[Thu Jun 05 10:15:40 2014] [error] [client ] Updated 1 rows.\n
But when I check the database the line has not been inserted.
Now if I copy the SQL and the values and run it manually it works.
And if a record is already in there it works it will update the total. But it will not insert the record.
To make it even weirder this sql is a function used all over the site and it works with no issues anywhere else. Just this one scenario.
your parameter array is incorrect. It should be 'BID'
, 'BST'
, 'BSC'
that is, drop the colon :
for the parameter array.
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.