[英]Re-using the data brought by mysql_fetch_array
我有一個名為books containsg表的表
book_id | 標題| 作者| 摘要
使用下面的代碼,我查詢db並顯示書名和作者的列表。 列出后,每當單擊一本書的標題鏈接時,我都想顯示該書的摘要,而無需再次查詢數據庫...如何在不重新查詢數據庫而僅使用$ books_query_result中存儲的數據的情況下完成此操作?
$books_query="SELECT * FROM books";
$books_query_result=mysql_query($books_query) or die("Could not get list".mysql_error());
echo "<table><tr>";
echo "<td>";
while($list=mysql_fetch_array($books_query_result)){
echo "<A href=\"" .$list['book_id'] ."\"> ". $list['title']. "</a>";
echo $list['author']."<br>";
}
# Now I want to reset and re-use mysql_fetch_array
mysql_data_seek($books_query_result, 0);
$summary=mysql_fetch_array($books_query_result);
echo "</td>";
echo "<td>";
#display summary here
echo "<td>";
echo "</tr></table>";
打印所有數據,使其看起來像是可見的,並將摘要匯總到帶有'style =“ display:none”的div中。 然后在鏈接上將調用添加到用於切換可見性的函數: http : //www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/ 。 確保您還將該函數添加到了javascript中。 這樣,您的數據將始終存在,您無需重新加載數據。
如果您要重新設置和迭代$ books_query_result,則應使用mysql_data_seek($ books_query_result,0);。 而是mysql_data_seek($ selectcompany_query_result,0)。
只需在while循環中准備數組,然后在任何地方使用它
while($list=mysql_fetch_array($books_query_result)){
//echo "<A href=\"" .$list['book_id'] ."\"> ". $list['title']. "</a>";
//echo $list['author']."<br>";
// prepare array here
$resultArray[] = $list;
}
現在使用$ resultArray。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.