无限循环的问题

发布于 2024-09-03 21:48:02 字数 1211 浏览 10 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

栀子花开つ 2024-09-10 21:48:02

问题是查询在循环中,因此每次都会运行(因此每次都从头开始)。只需将 mysql_query() 部分移至 while 循环之前并将其存储在变量中:

$query = mysql_query("SELECT * FROM users");
while($row=mysql_fetch_array($query))

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:

$query = mysql_query("SELECT * FROM users");
while($row=mysql_fetch_array($query))
流绪微梦 2024-09-10 21:48:02

您可以用一根线替换这个大型开关:

$subid = rand(($catid * 8) - 7, min($catid * 8, 70));

You can replace this mega switch with one line:

$subid = rand(($catid * 8) - 7, min($catid * 8, 70));
只涨不跌 2024-09-10 21:48:02

while 循环的条件在每次迭代时执行和评估。因此每次迭代都会调用 mysql_query 并返回 true。

只需执行一次数据库查询并缓存结果:

function addAds($n) {
    $result = mysql_query("SELECT * FROM users");
    $aut = array();
    while ($row = mysql_fetch_array($result)) {
        $aut[]=$row['name'];
    }
    $rowCount = count($aut);
    for ($i=0; $i<=$n; $i++) {
        $author=$aut[rand(0,$rowCount)];
        // …
        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 condition of a while loop is executed and evaluated with each iteration. So mysql_query is called with every iteration and retunrs true.

Just execute your database query once and cache the result:

function addAds($n) {
    $result = mysql_query("SELECT * FROM users");
    $aut = array();
    while ($row = mysql_fetch_array($result)) {
        $aut[]=$row['name'];
    }
    $rowCount = count($aut);
    for ($i=0; $i<=$n; $i++) {
        $author=$aut[rand(0,$rowCount)];
        // …
        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.";
}
彡翼 2024-09-10 21:48:02

我还认为问题是你的函数太大而无法(快速)理解。您应该使它们更小并使用像 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.

冷弦 2024-09-10 21:48:02

我很多时候不使用 PHP,但我认为赋值

$row=mysql_fetch_array(mysql_query("SELECT * FROM users"))

应该始终返回 true,它在每次迭代时一次又一次地执行查询。

It's a lot of time that I don't use PHP but I think that the assignment

$row=mysql_fetch_array(mysql_query("SELECT * FROM users"))

should always returns true, it executes the query again and again on every iteration..

十二 2024-09-10 21:48:02

每次运行循环时都会启动一个新查询。

You're starting a new query each time you run the loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文