私は気が狂う原因となっている奇妙な問題に直面しています。私はこのようなことに出くわすことはありません。本当に簡単なものになると予測できますが、見過ごしています。
ビジネス ロール ロジックに基づいてクエリを実行しています。そのため、選択して更新する場合もあれば、選択して更新してから挿入する場合もあります。コードの処理方法に関する全体的な構造は次のとおりです
Open PDO connection
1 getDataSet 1 (select)
2 ProcessQuery (update)
3 getDataSet 1 (select)
4 ProcessQuery (Insert)
Close PDO connection by setting the PDO object to null.
奇妙な理由で、挿入がまったく機能しません! 選択は機能し、更新は問題なく機能しますが、挿入部分に関しては機能しません。エラーもまったく発生しません。実際、同じクエリをコピーして phpMyAdmin で実行したところ、クエリが機能しました。
挿入しようとしているテーブルは非常に大きく、多くのインデックスを持つ 400 万を超えるレコードがあることに注意してください。
何が問題なのですか?他に何を確認できますか?挿入がスクリプトから機能せず、phpmyadmin から機能するのはなぜですか??
ここに私のコードがあります
これは、サーバーへの接続に使用する現在の PDO クラスです http://pastebin.com/XQ2RrhA1
<?php
class connection {
private $connString;
private $userName;
private $passCode;
private $server;
private $pdo;
private $errorMessage;
protected $lastQueryTime;
protected $lastQuery;
private $pdo_opt = array (
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
);
function __construct($dbName, $serverName = 'localhost'){
//sets credentials
$this->setConnectionCredentials($dbName, $serverName);
//start the connect
$this->startConnection();
}
function startConnection(){
$this->pdo = new PDO($this->connString, $this->userName, $this->passCode, $this->pdo_opt);
if( ! $this->pdo){
$this->errorMessage = 'Failed to connect to database. Please try to refresh this page in 1 minute. ';
$this->errorMessage .= 'However, if you continue to see this message please contact your system administrator.';
echo $this->getError();
}
}
//this will close the PDO connection
public function endConnection(){
$this->pdo = null;
}
//return a dataset with the results
public function getDataSet($query, $data = NULL)
{
$start = microtime(true);
$cmd = $this->pdo->prepare( $query );
$cmd->execute($data);
$ret = $cmd->fetchAll();
//$cmd->closeCursor();
$this->lastQueryTime = microtime(true) - $start;
$this->lastQuery = $query;
return $ret;
}
public function processQuery($query, $data = NULL)
{
$start = microtime(true);
//$this->pdo->beginTransaction();
$cmd = $this->pdo->prepare( $query );
$ret = $cmd->execute($data);
//$this->pdo->commit();
//$cmd->closeCursor();
$this->lastQueryTime = microtime(true) - $start;
$this->lastQuery = $query;
return $ret;
}
//return last insert id
public function lastInsertId($name = NULL) {
if(!$this->pdo) {
return false;
}
return $this->pdo->lastInsertId($name);
}
public function getOneResult($query, $data = NULL){
$cmd = $this->pdo->prepare( $query );
$cmd->execute($data);
return $cmd->fetchColumn();
}
public function getError(){
if($this->errorMessage != '')
return $this->errorMessage;
else
return true; //no errors found
}
//this where you need to set new server credentials with a new case statment
function setConnectionCredentials($dbName, $serv){
switch($serv){
case default:
$this->connString = 'mysql:host='.$serv.';dbname='.$dbName.';charset=utf8';
$this->userName = 'USER';
$this->passCode = 'PASSWORD';
break;
}
}
public function lastQueryTime() {
if(!$this->lastQueryTime) {
throw new Exception('no query has been executed yet');
}
return $this->lastQueryTime;
}
public function lastQuery() {
if(!$this->lastQuery) {
throw new Exception('no query has been executed yet');
}
return $this->lastQuery;
}
}
?>
これは私の実際のコードです
<?php
require('../classes/connection.php');
$db = new connection(DATABASE_NAME, DATABASE_HOST);
$sendUpdate = 0;
$id = 0;
$resultCode = 0;
$callCode = 0;
$total_attempts = 0;
$account_id = 0;
$timer = 0;
$notes = '';
$triggerOn = '';
$subject = '';
if(isset($_POST['sendUpdate'])){
$sendUpdate = 1;
}
if(isset($_POST['current_call_id'])){
$id = bigint($_POST['current_call_id']);
}
if(isset($_POST['result_code_menu'])){
$resultCode = bigint($_POST['result_code_menu']);
}
if(isset($_POST['selected_call_code'])){
$callCode = bigint($_POST['selected_call_code']);
}
if(isset($_POST['total_attempts'])){
$total_attempts = bigint($_POST['total_attempts']);
}
if(isset($_POST['account_id'])){
$account_id = bigint($_POST['account_id']);
}
if(isset($_POST['notes'])){
$notes = trim($_POST['notes']);
}
if(isset($_POST['triggerOn'])){
$triggerOn = convertTimeToUTCzone( $_POST['triggerOn'], USER_TIME_ZONE );
}
$subject = $resultCode;
if(isset($_POST['timer'])){
$timer = convertTimeToSeconds($_POST['timer']);
}
//CONVERT $time to seconds
$error_list = '';
$pass_message = '';
if($id < 1){
$error_list .= '<li>You have selected an invalid link</li>';
}
if($callCode == 0){
$error_list .= '<li>You must select a call code.</li>';
}
if($resultCode == 0){
$error_list .= '<li>You must select a result code.</li>';
}
if($timer == 0){
$error_list .= '<li>You can not reset timer before submitting the form.</li>';
}
//if pass all check
if($error_list == ''){
$pass_all = 0;
//Find out what is the next action
$action = $db->getDataSet('SELECT result FROM result_codes WHERE result_code_id = '.$resultCode.' LIMIT 1;' );
if( count($action) == 1){
$next_action = $action[0]['result'];
} else {
$error_list .= '<li>Error #95: Unknown Error: result code was not found.</li>';
$pass_all = 0;
}
//Close existing open phone call
if( $next_action == 'FINISH' || $next_action == 'CREATE NEW CALL' || $next_action == 'TRY AGAIN' ){
$statment = $db->processQuery('UPDATE phone_calls SET result_code_id= ?, call_notes= ?, call_duration = ?,
first_attempt_on = if(first_attempt_on IS NULL, NOW(), first_attempt_on),
first_attempt_by = if(first_attempt_by = "", '.USER_ID.',first_attempt_by),
last_attempt_on = NOW(), total_attempts = total_attempts+1, status=2 WHERE phone_call_id = '.$id.' LIMIT 1;'
, array($resultCode, $notes, $timer) );
if($statment){
$pass_all = 1;
} else {
$error_list .= '<li>Error #96: System could not update Phone Call</li>';
$pass_all = 0;
}
$statment = null;
}
//Update the existing phone call & Keep it open to be called again
if( $next_action == 'TRY AGAIN'){
$new_call = $db->getDataSet('SELECT call_code_title AS subject FROM call_codes WHERE call_code_id= '.$callCode.' LIMIT 1;' );
if( count($new_call) == 1 ){
$subject = $new_call[0]['subject'];
} else {
$error_list .= '<li>Error #79: Unknown Error: call code was not found.</li>';
}
$new_call = null;
$this_attempt = $total_attempts+1;
if($this_attempt >= 1){
$subject = $subject . ' attempt: ' . $this_attempt;
}
$statment = $db->processQuery('INSERT INTO phone_calls (account_id, call_code_id, trigger_on, created_on, call_subject, status, last_call_id
, call_direction, owner_id, workflow_generated, call_notes)
VALUES('.$account_id.', '.$callCode.', "'.$triggerOn.'", NOW(), "'.$subject.'", 1, '.$id.', "OUTBOUND", '.USER_ID.', 1, "");');
if($statment ){
$pass_all = 1;
} else {
$error_list .= '<li>Error #80: System could not generate a new attempt</li>';
$pass_all = 0;
}
$statment = null;
}
//Update the existing phone call THEN assign the activity to the master user define in APP_configuration.php
if( $next_action == 'MGR REVIEW'){
$statment = $db->processQuery('UPDATE phone_calls SET result_code_id= ?, call_notes= ?, call_duration = ?,
first_attempt_on = if(first_attempt_on IS NULL, NOW(), first_attempt_on),
first_attempt_by = if(first_attempt_by = "", '.USER_ID.',first_attempt_by),
last_attempt_on = NOW(), total_attempts = total_attempts+1,
trigger_on = ?, owner_id = '.CMS_ADMIN_ID.' WHERE phone_call_id = '.$id.' LIMIT 1;'
, array($resultCode, $notes, $timer, $triggerOn) );
if($statment){
$pass_all = 1;
} else {
$error_list .= '<li>Error #98: System could not update Phone Call</li>';
$pass_all = 0;
}
$statment = null;
}
if($sendUpdate == 1 && $error_list == '' && $pass_all == 1 ){
$statment = $db->processQuery('DELETE FROM phone_calls WHERE last_call_id = '.$id.' LIMIT 1;');
if($statment){
$pass_all = 1;
} else {
$error_list .= '<li>Error #81: System could not run reverse system flow.</li>';
$pass_all = 0;
}
$statment = null;
}
//Generate new phone call
if( $next_action == 'CREATE NEW CALL'){
//Find the nect call code to generate
$new_call = $db->getDataSet('SELECT ie.action_id, CONCAT(cc.call_code_name, " - ", cc.call_code_title) AS subject FROM inventory_engine AS ie
INNER JOIN call_codes AS cc ON ie.action_id = cc.call_code_id
WHERE ie.call_code_id= ? AND ie.result_code_id = ? LIMIT 1;', array($callCode,$resultCode ) );
if( count($new_call) == 1 ){
$new_callcode_id = $new_call[0]['action_id'];
$subject = $new_call[0]['subject'];
} else {
$error_list .= '<li>Error #94: Unknown Error: call code was not found.</li>';
}
$new_call = null;
$statment = $db->processQuery('INSERT INTO phone_calls (account_id, call_code_id, trigger_on, created_on, call_subject, status, last_call_id
, call_direction, owner_id, workflow_generated, call_notes)
VALUES(?, ?, ?, NOW(), ?, 1, ?, "OUTBOUND", ?, 1, "");',
array($account_id, $new_callcode_id, $triggerOn, $subject, $id, USER_ID ) );
if($statment){
$pass_all = 1;
} else {
$error_list .= '<li>Error #99: System could not update Phone Call</li>';
$pass_all = 0;
}
$statment = null;
}
if($pass_all == 1 && $error_list == ''){
$pass_message = '<li>You have successfully complete the phone call.</li>';
} else {
$error_list .= '<li>Error #100: Unknown Error: Please contact your system admin</li>';
}
}
//close database connection
$db->endConnection();
$return = array();
if($pass_message != ''){
$return['msg'] = '<ul>'.$pass_message.'</ul>';
$return['error'] = false;
} else {
$return['msg'] = '<ul>'. $error_list.'</ul>';
$return['error'] = true;
}
echo json_encode($return);
?>
これは、レコードを挿入しない実際のコードです
ご協力いただきありがとうございます :)
PSこれは私のコードでは実行されないクエリですが、PHPmyadminで実行すると実行されます
INSERT INTO phone_calls (account_id, call_code_id, trigger_on, created_on, call_subject, status, last_call_id , call_direction, owner_id, workflow_generated, call_notes) VALUES(11601, 1, "2013-04-11 16:36:00", NOW(), "Initial Development attempt: 1", 1, 17132, "OUTBOUND", 1, 1, "");