問(wèn)題描述
我正在嘗試使用 MySQl 準(zhǔn)備好的語(yǔ)句從數(shù)據(jù)庫(kù)中獲取行并獲得結(jié)果.然而,這是行不通的.
請(qǐng)有人知道我哪里出錯(cuò)了嗎?我已經(jīng)嘗試了幾個(gè)小時(shí)的解決方案,但我無(wú)法讓它發(fā)揮作用.該頁(yè)面不會(huì)加載,就好像查詢(xún)失敗一樣.
$tag = trim($_GET['tag']);$stmt = $mysqli->prepare('SELECT posts.* FROM tags JOIN posts ON posts.id = tags.post_id WHERE tag = ?');$stmt->bind_param('s', $tag);$stmt->execute();$stmt->store_result();$result = $stmt->get_result();while ($row = $result->fetch_assoc()) {回聲 $row['tag'];}$stmt->free_result();$stmt->close();
試試這個(gè):
$stmt = $mysqli->prepare('SELECT posts.id FROM tags JOIN posts ON posts.id = tags.post_id WHERE tag = ?');...$stmt->bind_result($id);而 ($stmt-> fetch()) {//var_dump 整行以確保您期望的密鑰可用var_dump($id);}
更新
如果你想做一個(gè)選擇 *,而不是單獨(dú)指定每一列,看看這個(gè) post(不是接受的答案,而是得分最高的答案).否則,我強(qiáng)烈建議您查看 PDO,因?yàn)樗惯@些基本的讀取操作變得更加容易.>
I am trying to get rows from the database using MySQl prepared statements and get result. However this is not working.
Please can someone see where I am going wrong? I have been trying solutions for hours but I can't get it to work. The page just doesn't load as if the query has failed.
$tag = trim($_GET['tag']);
$stmt = $mysqli->prepare('SELECT posts.* FROM tags JOIN posts ON posts.id = tags.post_id WHERE tag = ?');
$stmt->bind_param('s', $tag);
$stmt->execute();
$stmt->store_result();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
echo $row['tag'];
}
$stmt->free_result();
$stmt->close();
Try this:
$stmt = $mysqli->prepare('SELECT posts.id FROM tags JOIN posts ON posts.id = tags.post_id WHERE tag = ?');
...
$stmt->bind_result($id);
while ($stmt->fetch()) {
// var_dump entire row to ensure the key you expect is avail
var_dump($id);
}
Upate
If you want to do a select *, vs having to specify EVERY column individually, check out this post (not the accepted answer, but the highest scoring answer). Otherwise I strongly urge you to check out PDO, as it makes these basic read ops much easier.
這篇關(guān)于雖然循環(huán) PHP get_result 不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!