gpt4 book ai didi

php - 代码有效,但需要很长时间才能加载 "Maximum execution time of 30 seconds exceeded"以及错误

转载 作者:行者123 更新时间:2023-11-29 07:18:02 24 4
gpt4 key购买 nike

它在第 7 行给出了数据库的输出以及上述错误:“超过 30 秒的最大执行时间”。

<?php
require_once 'config.php';//content below
$sql = "select *from users";
$selected = $mysqli->query($sql);
$row = "random";
while($row!=="null"){
$row = $selected->fetch_assoc();
echo $row['username'];
}

在 config.php 中:

<?php
DEFINE('DB_server','localhost');
DEFINE('DB_username','root');
DEFINE('DB_password','');
DEFINE('DB_name','demo');
$mysqli = new mysqli(DB_server,DB_username,DB_password,DB_name);
if($mysqli===NULL){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
?>

最佳答案

您正在创建一个无限循环。 $row 永远不会等于字符串 "null",因为 $row 是数组或字符串 "random"

两件事。首先,您应该启用 error reporting for MySQLi .它应该始终打开。在 new mysqli 之前使用 mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

<?php
DEFINE('DB_server','localhost');
DEFINE('DB_username','root');
DEFINE('DB_password','');
DEFINE('DB_name','demo');

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli(DB_server,DB_username,DB_password,DB_name);
?>

其次,当您想循环查询结果时,您可以简单地使用 foreach。

<?php
require_once 'config.php';//content below
$sql = "select *from users";
$selected = $mysqli->query($sql);
foreach($selected as $row) {
echo $row['username'];
}

关于php - 代码有效,但需要很长时间才能加载 "Maximum execution time of 30 seconds exceeded"以及错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58157910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com