問題描述
誰能解釋一下原因
$sql->execute($params);
返回FALSE
,而
print $pdo->errorCode();
print_r($pdo->errorInfo());
都返回SQLSTATE
00000
,表示根據(jù)文檔成功?它是一個 INSERT
并且實際上沒有將任何內(nèi)容插入到數(shù)據(jù)庫中...那么,為什么我會從 SQLSTATE
收到成功消息?
both return SQLSTATE
00000
, which means according to the documentation success? It is an INSERT
and nothing is actually being inserted into the database... so, why do I get a success message from SQLSTATE
?
如果有幫助,這是代碼...
In case it helps, this is the code...
$sql = $pdo->prepare("
INSERT INTO user (
username, fname, pass, salt, email,
loc_id_home, country_id_home, region_id_home,
cont_id_home, timestamp_reg, timestamp_upd, timestamp_lastonline,
online_status, gender, birthdate
)
VALUES (
:username,:fname,:pass,:random_salt,:email,
:loc_id_home,:country_id_home,:region_id_home,
:cont_id_home,'".time()."','".time()."','".time()."',
1,:gender,:birthdate)
");
$params=array(
':username'=>$username,
':fname'=>$fname,
':pass'=>$pass,
':random_salt'=>$random_salt,
':email'=>$email,
':loc_id_home'=>$loc_id_home,
':country_id_home'=>$country,
':region_id_home'=>$region,
':cont_id_home'=>$continent,
':gender'=>$gender,
':birthdate'=>$birthdate
);
$sql->execute($params);
print $pdo->errorCode();
print_r($pdo->errorInfo());
推薦答案
這是因為 $pdo->errorInfo()
指的是最后一條成功執(zhí)行的語句.由于 $sql->execute()
返回 false,所以它不能引用該語句(要么不引用,要么引用之前的查詢).
It is because $pdo->errorInfo()
refers to the last statement that was successfully executed. Since $sql->execute()
returns false, then it cannot refer to that statement (either to nothing or to the query before).
至于為什么 $sql->execute()
返回 false,我不知道...要么是您的 $params
數(shù)組有問題,要么與您的數(shù)據(jù)庫連接.
As to why $sql->execute()
returns false, I don't know... either there is a problem with your $params
array or with your database connection.
PDO::errorCode — 獲取與數(shù)據(jù)庫句柄上的最后一個操作相關(guān)聯(lián)的 SQLSTATE
PDO::errorCode — Fetch the SQLSTATE associated with the last operation on the database handle
注意:PHP 手冊(http://php.net/manual/en/pdo.errorinfo.php) 并沒有準確定義對數(shù)據(jù)庫句柄的最后操作"是什么意思,但是如果綁定參數(shù)存在問題,那么該錯誤就會發(fā)生在 PDO 內(nèi)部,并且不會與數(shù)據(jù)庫進行任何交互.可以肯定地說,如果 $pdo->execute()
返回 true
,則 $pdo->errorInfo()
是有效的.如果 $pdo->execute()
返回 false
,則 $pdo->errorInfo()
的行為沒有從文檔.如果我的經(jīng)驗沒有記錯的話,execute 返回 true
,即使 MySQL 返回錯誤,如果未執(zhí)行任何操作,則返回 false
.由于文檔不是特定的,它可能是特定于數(shù)據(jù)庫驅(qū)動程序的.
Note: The PHP manual (http://php.net/manual/en/pdo.errorinfo.php) does not define exactly what "last operation on the database handle" means, but if there was an issue with binding parameters, that error would have occurred inside PDO and without any interaction with the database. It is safe to say that if $pdo->execute()
returns true
, that $pdo->errorInfo()
is valid. If $pdo->execute()
returns false
, the behavior of $pdo->errorInfo()
is not explicitly clear from the documentation. If I recall correctly from my experience, execute returns true
, even if MySQL returned an error, returns false
if no operation was done. Since the documentation is not specific, it might be db driver specific.
此答案反映了截至 2012 年 9 月撰寫時的實際經(jīng)驗.正如用戶所指出的,文檔并未明確重申這種解釋.它也可能只反映特定的數(shù)據(jù)庫驅(qū)動程序?qū)崿F(xiàn),但如果 $pdo->execute()
返回 true
,則 $pdo 應(yīng)該總是正確的->errorInfo()
有效.
This answer reflects practical experience as of when it was written in September 2012. As a user has pointed out, the documentation does not explicitly reaffirm this interpretation. It also may only reflect the particular database driver implementation, but it should always be true that if $pdo->execute()
returns true
, that $pdo->errorInfo()
is valid.
您可能還想在連接序列中設(shè)置 PDO::ERRMODE_EXCEPTION.異常處理使得不需要檢查和查詢錯誤.
You might also want to set PDO::ERRMODE_EXCEPTION in your connect sequence. Exception handling makes it unnecessary to check and query the error.
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
這篇關(guān)于PDO SQL 狀態(tài)“00000"但仍然錯誤?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!