I have searched here for answer to my problem. Some might had already answered here with other versions or drivers. This answer would help me a lot to go forward in my developments.
My PDOStatement::prepare()
is returning true. But my PDOStatement::execute();
returning false.
Whereas, The rows are inserting successfully in the database. I have searched for the problem in php.net as well as PDO documentations. My search went in vein.
My simple insert code is as follows:
// get current rank into a variable //
$sql = 'SELECT count(email)+1 AS RANK FROM users_main';
$pre = $dbc->prepare($sql);
if($pre){
$pre->execute(array($email)); // <-- working good ...
$res = $pre->fetchObject();
if($res)$rank=(string)$res->RANK;else $rank='';
}
// now inserting new user into the table as per his new rank.
$usercode = substr(md5(gzencode($email)),0,10);
$activationcode = md5(gzencode(substr(md5($email),0,10)));
// inserting the actual row into db ...
$sql = "INSERT INTO users_main(email,usercode,activation_code,current_rank) VALUES (?,?,?,?)";
$pre = $dbc->prepare($sql);
// debugging ...
echo '<pre>';
try {
$pre->execute(array($email,$usercode,$activationcode,$rank)); // <-- PROBLEM IS HERE . RETURNING FALSE.
}catch(Exception $e){
echo $e->getMessage(); // <-- NO ERROR MESSAGE
}
echo '<br><br>';
print_r($dbc->errorInfo()); // <-- here sqlstate is 0000
echo '</pre>';
/*$emailSubject = 'Welcome Mail!';
$emailLink = '?b='.$activationCode;
if(insertEmailQueue('W',$email,$emailSubject,$emailLink))return true;else return false;
}*/
Adding to the above information, I have checked the datatype of each columns I am trying to insert into. Everything looks fine. Replying this question would really help me a lot.
I was debugging above code and I was trying to change to get what is the error. I have changed the code as below :
try {
$res = $pre->execute(array($email,$usercode,$activationcode,$rank)); // <-- PROBLEM IS HERE . RETURNING FALSE.
}catch(PDOException $e){
echo $e->getMessage(); // <-- NO ERROR MESSAGE
}
echo '<br><br>';
print_r($dbc->errorInfo()); // <-- here sqlstate is 0000
echo '<br><br>';
print_r($dbc->errorCode());
echo '<br><br>';
print_r($pre->errorCode());
echo '</pre>';
The result was as below :
Array
(
[0] => 00000
[1] =>
[2] =>
)
00000
23000 // <-- this is a fatal error as per the documentation.
After a two days long struggle to know what the error was, finally I found by trying to get error and other internet references. What I have changed is
....
// now inserting new user into the table as per his new rank.
$usercode = addslashes(substr(md5(gzencode($email)),0,10));
$activationcode = addslashes(md5(gzencode(substr(md5($email),0,10))));
....
addslashes()
is one of the primitive way to escape the unacceptable characters.
I still wonder, why did MySQL accept the statement, insert the rows and returned false after inserting. I will update the answer if I find out why this weird problem occurs.
Thank you every one for helping me.
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.