2017-06-29 121 views
0

我想某種錯誤處理設置一個布爾變量FALSE如果語句錯誤 或設定布爾如果它運行沒有錯誤如何檢查語句是否執行錯誤或沒有錯誤?

執行真正的在我的病情我的代碼是

$response = array(); 
$response["success"] = true; 

$statement = $mysqli->prepare("INSERT INTO userft (name, username, pasword, email, contact, age) VALUES (?, ?, ?, ?, ?, ?)"); 
$statement->bind_param('ssssii', $name, $username,$pasword,$email,$contact, $age); 
$statement->execute(); 

if(!($statement->execute())){ //<----- in replace of this statement?<----- 
$response["success"]=false;}  
+0

http://php.net/manual/en/mysqli.error.php,http://php.net/manual/en/mysqli-stmt.error.php – ADyson

+1

'$陳述書>的execute()'execting插圖/ UPDATE時/ DELETE已經回到一個布爾真或假 – RiggsFolly

+0

我認爲這是對數據庫連接 檢查錯誤,如果我錯了,請修改我的代碼:)謝謝 –

回答

3

如果您將execute語句返回值分配給您可以測試成功/失敗的變量。

$response = array('success'=>false); 


$stmt = $mysqli->prepare("INSERT INTO userft (name, username, pasword, email, contact, age) VALUES (?, ?, ?, ?, ?, ?)"); 
/* First test that the statement was prepared successfully */ 
if($stmt){ 
    $stmt->bind_param('ssssii', $name, $username,$pasword,$email,$contact, $age); 

    /* assign return value of execute to a variable */ 
    $status = $stmt->execute(); 

    /* use variable to determine response */ 
    $response["success"]=$status ? true : false; 
} 

你可以閱讀更多關於prepared statementsPHP site

+0

這將是很好,包括一個鏈接到[mysqli_prepare文檔】(HTTP ://php.net/manual/en/mysqli.prepare.php) –

1

的執行語句返回布爾值,因此你在做正確的事;您可以將結果分配給一個變量:

$success = $statement->execute(); 
if ($success) { 
    echo "Inserted!"; 
} else { 
    echo "Something went wrong!"; 
} 

如果有疑問,請檢查documentation

1

試試這個

$statement->execute(); 
if ($statement->affected_rows()) { 
     //success 
} else { 
    die('execute() failed: ' . $mysqli->error); 
}