PHP中的一個while迴圈
PHP code
while($rows=mysql_fetch_array(mysql_query("select pl_title from pagelinks limit 1,2"))){ $string=$rows['pl_title']; $string1=urlencode($string);}echo $string;$url = "http://localhost/index.php/"."$string1";$contents = file_get_contents($url);if((preg_match_all('/()/iUs', $contents, $match))){$contents = $match[1][0];}else{ (preg_match_all('/()/iUs',$contents,$match)); $contents = $match[1][0]; //echo $match[1][0]; }
這樣處理2條資料,都會超過30秒逾時,是語句錯了嗎?目的是批量處理資料,用if處理一條一條的資料,就沒有問題。
------解決方案--------------------
while($rows=mysql_fetch_array(mysql_query("select pl_title from pagelinks limit 1,2")))
想想看這個運算式的值會否變成false,每次迴圈都會執行mysql_query("select pl_title from pagelinks limit 1,2"),那麼你獲得的結果就永遠只是第一條,陷入死迴圈
------解決方案--------------------
你把mysql_query()分離出來應該不會逾時。你那樣寫陷入死迴圈了。
------解決方案--------------------
每次迴圈都會執行while($rows=mysql_fetch_array(mysql_query("select pl_title from pagelinks limit 1,2")))
覺得就是分開寫吧
$sql="select pl_title from pagelinks limit 1,2";
$rs=mysql_query($sql);
.....
而且while{}應該從頭到尾吧,while(..){
$string=$rows['pl_title'];
$string1=urlencode($string);
echo $string;
$url = "http://localhost/index.php/"."$string1";
$contents = file_get_contents($url);
if((preg_match_all('/()/iUs', $contents, $match))){
$contents = $match[1][0];
}
else{
(preg_match_all('/()/iUs',$contents,$match));
$contents = $match[1][0];
//
}
不然在while裡面出不來了,就是死迴圈了
------解決方案--------------------
還是得把mysql_query()拿出來
------解決方案--------------------
$query = mysql_query("select pl_title from pagelinks limit 1,3");
while ($rows=mysql_fetch_array($query)){
$string[]=$rows['pl_title'];
$string1=urlencode($string);
}
print_r($string);
------解決方案--------------------
探討
PHP code
$query = mysql_query("select pl_title from pagelinks limit 1,3");
while ($rows=mysql_fetch_array($query)){
$string=$rows['pl_title'];
$string1=urlencode($string);
}
echo $string;
……
------解決方案--------------------
迴圈裡面執行sql語句可是個效率很低的方法,建議換個別的方法!
------解決方案--------------------
PHP code
)/iUs', $contents, $match))){ $contents = $match[1][0]; } else{ preg_match_all('/()/iUs',$contents,$match); $contents = $match[1][0]; } // 串連的代碼被刪除了,你只需要改變串連到的資料庫在while上一行 mysql_query("set names 'utf8'"); // 下面兩句是什麼意思? // $sql=""; // mysql_query($sql); $SQL=" INSERT INTO pagecontents (old_title,old_text) VALUES('{$string}','{$contents}')"; // 查詢,失敗返回錯誤訊息 mysql_query($SQL) or die(mysql_error());}// while迴圈結束?>