PHP无限循环问题

我可以使用正确的代码来工作,但是我希望能够使用对象和方法,但是这是行不通的.重复数据库中的相同条目,直到查询崩溃.我看到其他人在while语句中有查询,但是我认为我使用的方法应该只查询一次该语句,但是很可能是错误的.谢谢.

<?php
include '/functions/MySQL.php';
$MySQL = new MySQL;
$con = mysqli_connect("host","user","password","db");
$result = mysqli_query($con,"SELECT * FROM reportLogger WHERE Moderator='jackginger'");
while($row = mysqli_fetch_array($MySQL->getReports('jackginger'))) {
    $time           = $row['Time'];
    $moderator      = $row['Moderator'];
    $reason         = $row['Reason'];

    // Now for each looped row

    echo "<tr><td>".$time."</td><td>".$moderator."</td><td>".$reason."</td></tr>";
}
?>

独立班

public function __construct(){
        $this->con = mysqli_connect("localhost","root","pass","Minecraft");
        // Check connection
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    }

    public function getUUID($username) {
        $result = mysqli_query($this->con,"SELECT UUID FROM loginLogger WHERE Username='" . $username . "'");
        return mysqli_fetch_array($result)[0];
    }

    public function getReports($username) {
        $result = mysqli_query($this->con,"SELECT * FROM reportLogger WHERE UUID='" . $this->getUUID($username) . "'");
        return $result;
    }

解决方法:

每次调用while($row = mysqli_fetch_array($MySQL-> getReports(‘jackginger’)))时,您都在进行新查询,因此它一次又一次地获取相同的内容.

一个解决方案可能是:

<?php
include '/functions/MySQL.php';
$MySQL = new MySQL;
$con = mysqli_connect("host","user","password","db");
$result = mysqli_query($con,"SELECT * FROM reportLogger WHERE Moderator='jackginger'");
$store = $MySQL->getReports('jackginger');
while($row = mysqli_fetch_array($store)) {
    $time           = $row['Time'];
    $moderator      = $row['Moderator'];
    $reason         = $row['Reason'];

    // Now for each looped row

    echo "<tr><td>".$time."</td><td>".$moderator."</td><td>".$reason."</td></tr>";
}
?>
上一篇:php-Opencart地址页面分页


下一篇:php-使用MAMP在Mac OS中通过终端连接到MYSQLi