我是面向對象PHP的新手。目前,我正在製作一個登錄腳本,並堅持要取回&以迴應結果。這是我的腳本:PHP如何在while循環之外回顯提取結果
$stmt = $db->mysqli->prepare("select User from `users` where User = ? AND Password = ?");
$stmt->bind_param('ss', $username, $password);
$stmt->execute();
$stmt->store_result();
$num = $stmt->num_rows;
$stmt->bind_result($username);
if ($num !== 1) {
echo 'no result';
} else {
echo 'Your username: '. $username ;
}
$stmt->close();
正如你可以看到我不是在腳本中獲取上述結果。我在$num !== 1
之前的while循環中嘗試使用$stmt->fetch()
。
但是,即使在while循環中不使用數組(只是$username
),獲取的結果仍然以數組形式存儲(我認爲)。 我想要的: echo選擇查詢OUTSIDE while循環的結果。正如有可能在老式的方式(假設只有1條結果,因此沒有必要while循環):
$result = mysqli_query($conn, $query);
$record = mysqli_fetch_array($result);
echo $record['username'];
對'$ stmt-> User'沒有喜悅,說屬性不存在。我用'$ username'代替它,並且工作。非常感謝 :) – Tom 2012-03-21 19:49:10