简体   繁体   English

MySQL和Do While循环超时

[英]MySQL and Do While loop timing out

This is timing out without echo-ing anything as well as not inserting into database. 这是超时的,没有回显任何内容,也没有插入数据库。 Can't see why. 不明白为什么。 Any ideas? 有任何想法吗?

Note It's to simply generate a number of code numbers, I'm aware that it's simple and insecure, it's being done on localhost only and the connection values are sound. 注意它只是简单地生成许多代码号,我知道它是简单且不安全的,它仅在localhost上完成并且连接值正确。

function populate_codes(){
  $con = mysql_connect(dbhost,dbuser,dbpass);
  mysql_select_db(dbname);
  $i = 0;
  do{
    $code = rand(10000000,99999999);
    $check = mysql_query('SELECT * FROM '.dbcodetable.' WHERE code = $code',$con);
    if(!$check){
        $insertcode = mysql_query('INSERT INTO '.dbcodetable.' (code,status) VALUES ($code,1)',$con);
        if($insertcode){
            echo $i.' - '.$code.'<br />';
            $i++;
        }
     }
  }
  while($i <= 1999);
  echo "------------------------<br /><strong>Complete</strong>";
}

You: 您:

  1. set $i to 0 $i设置为0
  2. generate a random number 产生一个随机数
  3. perform an SQL query 执行SQL查询
  4. check if the query execution returned false 检查查询执行是否返回false
  5. only if it did, and some more conditions apply, are you finally incrementing $i 只有这样做了,并且需要满足一些其他条件,您才可以最终增加$i
  6. you repeat this until $i is incremented beyond 1999 您重复此操作,直到$i超过1999年为止

The problem is that your 4th step always succeeds . 问题是您的第四步总是成功 The query always executes successfully, mysql_query always returns a result set . 查询总是成功执行, mysql_query总是返回结果集 You have to inspect the result set to see what's in it, not just test it against == false . 您必须检查结果集以查看其中的内容,而不仅仅是对== false测试。 So, $i never increments. 因此, $i从不递增。

You also want to increment $i outside all those ifs, or have a general protection against infinite loops. 您还希望在所有这些$i之外增加$i ,或者对无限循环有一般的保护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM