2012-03-20 196 views
1

是否有可能做一些事情,就像我們在準備語句中正常mysqli查詢一樣,在lopping中。 例如while循環在mysqli準備語句

$sql = "SELECT * FROM users"*; 
$query = mysqli_query($link, $sql); 
while($row = mysqli_fetch_assoc($query)){ 
    echo $row['username']; 
    echo $row['name']; 
} 

在準備好的語句,它是這樣的

$fetch_comments = $friend_zone->prepare("SELECT item_poster, comment FROM status_comments WHERE status_id = ? and item_poster = ?"); 
$fetch_comments->bind_param("is", $status__id, $item_poster); 
$fetch_comments->execute(); 
$fetch_comments->store_result(); 
$fetch_comments->bind_result($item_poster_fetched, $comment_fetched); 
while($fetch_comments->fetch(){ 
    echo $item_poster; 
} 

我的意思是我想做的事情,像這樣

echo $row['something']; 

我想出了一個解決方案的回聲現在是使用綁定結果獲取它們,然後將它們放入循環內的數組中,然後將其放入循環內的數組中,然後將其放入循環中,然後將其放入循環內的數組中,或者類似的東西。 有沒有更好的方法?

回答

1

你幾乎在那裏與您的代碼。你只需要while循環的條件檢查中,指派價值$行:

while($row = $fetch_comments->fetch()) { 
    echo $row['somefield']; 
} 

現在的數據綁定到從我瞭解的$item_poster_fetched變量...

while($fetch_comments->fetch()) 
{ 
    $item_poster_fetched['something']; 
} 
+0

感謝我有點困惑,因爲手冊中的while循環的例子是一個稍微有點不同的 – 2012-03-20 09:43:35

+0

@cillosis'mysqli_stmt :: fetch'返回一個布爾值,請再次閱讀問題。 – xdazz 2012-03-20 09:45:22

+1

@xdazz謝謝你指出,我不知道這種行爲。我想我已經習慣了PDO。 – 2012-03-20 09:49:45

2

這不工作?

$fetch_comments = $friend_zone->prepare("SELECT item_poster, comment FROM status_comments  WHERE status_id = ? and item_poster = ?"); 
$fetch_comments->bind_param("is", $status__id, $item_poster); 
$fetch_comments->execute(); 
$result = $fetch_comments->get_result(); 
while ($row = $result->fetch_assoc()) { 

    echo($row['something']); 

} 
+0

即時消息我會試試看,我認爲這種方式不會工作,因爲在手冊中的例子是不同的... – 2012-03-20 09:43:13

+0

我沒有mysqlnd驅動程序,那麼還有其他解決方案嗎? – 2012-03-20 09:58:03

+1

所以在這種情況下$ fetch_comments-> get_result();指令失敗...所以可能你最初提出的解決方案工作得很好...... – 2012-03-20 10:27:22