无限循环的问题
function addAds($n) {
for ($i=0;$i<=$n;$i++) {
while($row=mysql_fetch_array(mysql_query("SELECT * FROM users"))) {
$aut[]=$row['name'];
}
$author=$aut[rand(0,mysql_num_rows(mysql_query("SELECT * FROM users")))];
$name="pavadinimas".rand(0,3600);
$rnd=rand(0,1);
if($rnd==0) {
$type="siulo";
} else {
$type="iesko";
}
$text="tekstas".md5("tekstas".rand(0,8000));
$time=time()-rand(3600,86400);
$catid=rand(1,9);
switch ($catid) {
case 1:
$subid=rand(1,8);
break;
case 2:
$subid=rand(9,16);
break;
case 3:
$subid=rand(17,24);
break;
case 4:
$subid=rand(25,32);
break;
case 5:
$subid=rand(33,41);
break;
case 6:
$subid=rand(42,49);
break;
case 7:
$subid=rand(50,56);
break;
case 8:
$subid=rand(57,64);
break;
case 9:
$subid=rand(65,70);
break;
}
mysql_query("INSERT INTO advert(author,name,type,text,time,catid,subid) VALUES('$author','$name','$type','$text','$time','$catid','$subid')") or die(mysql_error());
}
echo "$n adverts successfully added.";
}
这个函数的问题是它永远不会加载。正如我注意到的,我的 while 循环导致了它。如果我评论它,一切都好。它必须从我的数据库中获取随机用户并将其设置为变量 $author。
function addAds($n) {
for ($i=0;$i<=$n;$i++) {
while($row=mysql_fetch_array(mysql_query("SELECT * FROM users"))) {
$aut[]=$row['name'];
}
$author=$aut[rand(0,mysql_num_rows(mysql_query("SELECT * FROM users")))];
$name="pavadinimas".rand(0,3600);
$rnd=rand(0,1);
if($rnd==0) {
$type="siulo";
} else {
$type="iesko";
}
$text="tekstas".md5("tekstas".rand(0,8000));
$time=time()-rand(3600,86400);
$catid=rand(1,9);
switch ($catid) {
case 1:
$subid=rand(1,8);
break;
case 2:
$subid=rand(9,16);
break;
case 3:
$subid=rand(17,24);
break;
case 4:
$subid=rand(25,32);
break;
case 5:
$subid=rand(33,41);
break;
case 6:
$subid=rand(42,49);
break;
case 7:
$subid=rand(50,56);
break;
case 8:
$subid=rand(57,64);
break;
case 9:
$subid=rand(65,70);
break;
}
mysql_query("INSERT INTO advert(author,name,type,text,time,catid,subid) VALUES('$author','$name','$type','$text','$time','$catid','$subid')") or die(mysql_error());
}
echo "$n adverts successfully added.";
}
The problem with this function, is that it never loads. As I noticed, my while loop causes it. If i comment it, everything is ok. It has to get random user from my db and set it to variable $author.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题是查询在循环中,因此每次都会运行(因此每次都从头开始)。只需将 mysql_query() 部分移至 while 循环之前并将其存储在变量中:
The problem is that the query is in the loop, so it gets run every time (so you start from the beginning every time). Just move the mysql_query() part to right before the while loop and store it in a variable:
您可以用一根线替换这个大型开关:
You can replace this mega switch with one line:
while
循环的条件在每次迭代时执行和评估。因此每次迭代都会调用 mysql_query 并返回 true。只需执行一次数据库查询并缓存结果:
The condition of a
while
loop is executed and evaluated with each iteration. Somysql_query
is called with every iteration and retunrs true.Just execute your database query once and cache the result:
我还认为问题是你的函数太大而无法(快速)理解。您应该使它们更小并使用像 phpunit 这样的单元测试框架来测试它们。
I also think the problem is your functions are way too big to understand(quickly). You should make them smaller and test them with a unit testing framework like phpunit.
我很多时候不使用 PHP,但我认为赋值
应该始终返回
true
,它在每次迭代时一次又一次地执行查询。It's a lot of time that I don't use PHP but I think that the assignment
should always returns
true
, it executes the query again and again on every iteration..每次运行循环时都会启动一个新查询。
You're starting a new query each time you run the loop.