【问题标题】:Error while running php code运行php代码时出错
【发布时间】:2013-11-06 08:44:52
【问题描述】:

我正在制作一个小型 PHP 应用程序,它使用一些数据来检查它是否与数据库的记录匹配(登录过程的原型),但它给了我一个(额外的垃圾数据错误)和评论时用于检查错误的标题行,它给了我致命的错误:

致命错误:C:\wamp\www\hh\login.php 第 22 行的最大执行时间超过 30 秒

守则:

<?php

header("Content-type: text/xml");

$host = "localhost";
$user = "muhammed";
$pass = "";
$database = "test";

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT * FROM info";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$name = "tahany";
$age  = 90;

while(true){
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
    $row = mysql_fetch_assoc($resultID);
    if ($row['Name'] == $name && $row['age'] == $age){
        $res = "login success";
        break;
    }else{
        $res = "failed to login";
    }
}
}

echo $res;

?>

【问题讨论】:

  • 好吧,您在“登录失败”中有一个无限循环。虽然(真)是线索!

标签: php web-services fatal-error


【解决方案1】:

你需要优化你的代码,不需要额外的for loop

while($row=mysql_fetch_assoc($resultID)){
    if ($row['Name'] == $name && $row['age'] == $age){
        $res = "login success";
    }else{
        $res = "failed to login";
    }
}


注意: mysql_* 函数已弃用,请尽快移至 mysqli_* 函数。

【讨论】:

    【解决方案2】:
    You getting fatal error because of infinite loop you are putting break in inner loop but outer loop is infinite.
    

    【讨论】:

      【解决方案3】:

      您可以(并且应该)从代码中删除 while (true) 语句。它不是必需的。这就是导致您超时的原因。 break 语句只中断内部 for 循环,而不是外部 while 循环。

      现在,while 循环的修复可能是这样的:

      $break_loop = false;
      while (!$break_loop ) {
          // Keep your existing code as-is.
          for (...) {
              if (...) {
                  ...
              } else {
                  ...
              }
          }
      
          // Always break the loop, whether or not the log-in was successful. 
          // We need to stop the while-loop anyhow.
          //
          // When the log-in was successful, we jumped out of the for-loop much
          // sooner.
          $break_loop = true;
      }
      

      所以我们使用一个临时变量来保持循环运行,直到变量设置为true。当我们在登录成功或所有尝试都失败时跳出for循环时会发生这种情况。

      但同样,while-loop 是不需要的,因为您的 for-loop 已经处理了它。

      【讨论】:

        【解决方案4】:

        这段代码不好用,但很有用 休息2;

        http://php.net/manual/en/control-structures.break.php

        【讨论】:

          【解决方案5】:

          如果需要增加 PHP Script 的超时时间。这样做

          <?php
          set_time_limit(0);
          

          实际问题出在您的 while 循环中。

          您的while 循环在无限状态下运行。尝试更改它,如 .永远记住while(true) 无限运行。

          $i=0;
          while($i==0){
          for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
              $row = mysql_fetch_assoc($resultID);
              if ($row['Name'] == $name && $row['age'] == $age){
                  $res = "login success";
                  break;
              }else{
                  $res = "failed to login";
              }
          }
          $i=1; // Changing the flag to 1 , so while condition fails
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-04-26
            • 2017-11-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-25
            相关资源
            最近更新 更多