php两个while循环吗,PHP中的一个while循环解决方案

博客内容讨论了PHP中while循环导致的超时问题,分析了原因在于循环内重复执行SQL查询,提出了解决方案,包括将SQL查询移出循环、使用变量保存查询结果,以及优化循环结构以避免死循环。同时,提到了批量处理数据时的效率问题和正确处理方式。
摘要由CSDN通过智能技术生成

PHP中的一个while循环

PHP codewhile($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<?php

$DBserver = "localhost";

$DBname = "wikidb";

$DBuser = "root";

$DBpassword = "";

// mysql 连接,下面代码都将使用这个

$con = mysql_connect("localhost","root","");

mysql_select_db("wikidb");

$query = mysql_query("select pl_title from pagelinks limit 1,3") or die(mysql_error() );

// 更换数据库

mysql_select_db("new", $con);

while ($rows=mysql_fetch_array($query))

{

$string =$rows['pl_title'];

$string1 =urlencode($rows['pl_title']);

// 下面仍是循环的一部分

$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("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循环结束

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值