本文介紹了優化foreach中的while和SQL的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我的代碼:
$text = '12-name-y-86';
$array = explode('-',$text);
foreach($array as $value) {
$sql = mysql_query("SELECT * FROM `table` WHERE `pid`='$value' ORDER BY `id` LIMIT 3");
echo '***'.$value.'***';
echo '<br />';
while($row = mysql_fetch_array($sql)) {
echo $row['title'];
echo '<br />';
}
echo '<br /><br />';
}
打印:
12
標題1
標題2
標題3
姓名
ti1
ti2
ti3
是
t1
t2
tle3
86
mytitle1
mytitle2
mytitle3
此代碼可以完全購買 $text 中的更多值,服務器已關閉!
This code work full buy for more values in $text , server has down !
推薦答案
嘗試在一個查詢中選擇所有記錄,如下所示:
Try to select all records in just one query, like this:
$text = '12-name-y-86';
$array = explode('-', $text);
$array = "'" . implode(',', $array) . "'";
$sql = mysql_query("SELECT * FROM `table` WHERE `pid` IN (' . $array . ') ORDER BY `id` LIMIT 3");
while($row = mysql_fetch_array($sql)) {
echo $row['title'];
echo '<br />';
}
echo '<br /><br />';
這篇關于優化foreach中的while和SQL的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!