PHP中的一個while迴圈解決方案

來源:互聯網
上載者:User
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迴圈結束?> 
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.