使用PHP PDO時,在預處理語句中調用execute可能會完全掛起腳本的執行。 PDO初始化如下:PHP PDO執行掛起執行
$db = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$db->setAttribute(PDO::ATTR_PERSISTENT, false);
據我所知,連接工作正常 - 沒有任何跡象表明它失敗。然後,我稍後調用prepare並執行,並且執行似乎停止。
echo "Hi"; //executes
$username = mysql_real_escape_string($_POST['username']);
echo "Still Here"; //executes
$password = md5(mysql_real_escape_string($_POST['password']));
echo "So far so good"; //executes
$stmt = $db->prepare("SELECT * FROM users WHERE Username=:username");
echo "Still good"; //executes
$stmt.execute(array(':username' => $username));
echo "Nope..."; //doesn't execute
if($stmt->rowCount() >= 1)
{
echo "Sorry, that username is already taken.";
}
我不確定究竟發生了什麼導致此錯誤,但任何幫助解決它將不勝感激。
更改'$ stmt.execute(array(':username'=> $ username));'to'$ stmt-> execute(array(':username'=> $ username));' –