問題描述
在以傳統方式進行查詢之前,我一直在使用分頁類,但現在我已經開始使用準備好的語句進行查詢,通過 $stmt->fetch() 語句,我不知道如何現在分頁.我一直在環顧四周并檢查 stackoverflow 文獻,似乎有一些現成的解決方案,一些命令或一些:sort-> 東西.
I have been using paginating classes when I did before the query the traditional way, but now that I have come to do the queries using prepared statements, via the $stmt->fetch() statement, I don't know how to paginate now. I have been looking around and checking the stackoverflow literature and it seems that there is some kind of ready solution for it, some commands or some : sort-> stuff.
你如何對這個循環中的內容進行分頁?準備好的語句世界"中是否有一些命令?任何顯示的鏈接、頁面或網絡?
How do you paginate what comes out of this loop? Are there some commands within the "world of prepared statements"? any link, page or web where that is shown?
while( $stmt->fetch() )
{
printf("%s%s%s%s", $Email, $Webpage, $Telephone, $Mobile);
}
推薦答案
方法一:get_result()
:
*注意:此方法僅適用于 PHP >= 5.3,具有 mysqlnd 本機驅動程序.
Method 1: get_result()
:
*Note: This method only works with PHP >= 5.3, having the mysqlnd native driver.
假設這是使用 MySQLi 完成的,并且您已通過 bind_result()
將這些變量綁定為結果變量,我將改為使用 get_result()
將其傳輸到 MySQLi 結果資源中,并將行作為數組提取到包含所有行的數組.然后使用通常用于數組數據的任何分頁方法或插件:
Assuming this was done with MySQLi and you have bound these variables as result vars via bind_result()
, I would instead use get_result()
to transfer it into a MySQLi result resource, and fetch the rows as arrays onto an array containing all rows. Then use any pagination method or plugin you would normally use on array data:
// Don't use bind_result()...
// execute your statement
$stmt->execute();
// Get result set into a MySQLi result resource
$result = $stmt->get_result();
// array to hold all results
$rowset = array();
// And fetch with a while loop
while ($row = $result->fetch_assoc()) {
$rowset[] = $row;
}
var_dump($rowset);
? ? var_dump($rowset);
現在將 $rowset
用作二維數組,您可以使用它使用任何對常規數組進行操作的分頁方法.
Now use $rowset
as a 2D array, with which you can use any pagination method that operates on regular arrays.
如果您沒有 mysqlnd 本機驅動程序(因此無法使用 get_result()
,請繼續使用 bind_result()
,但將所有這些附加到數組中:
If you don't have the mysqlnd native driver (and hence cannot use get_result()
, continue using bind_result()
but append all of those onto an array:
// array to hold all rows
$rowset = array();
// All results bound to output vars
while ($stmt->fetch()) {
// Append an array containing your result vars onto the rowset array
$rowset[] = array(
'email' => $Email,
'webpage' => $Webpage,
'telephone' => $Telephone,
'moblile' => $Mobile
);
}
var_dump($rowset);
這篇關于使用準備好的語句編寫查詢的分頁結果集,的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!