3

I can get the correct code to work, but i want to be able to use objects and methods, which doesn't work. The same entry in the database is repeated until the query crashes. I saw other people that had queries inside of the while statement, but i thought that the method i am using should only query the statement once, but im likely wrong. Thanks.

<?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>";
}
?>

Seperate class

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;
    }
3
  • You need to store $MySQL->getReports('jackginger') in a separate variable, then call it with mysqli_fetch_array($your_temp_var). Commented Jul 13, 2014 at 4:45
  • Is it just because the query is getting called over and over again? Commented Jul 13, 2014 at 4:46
  • Yes and the reason is because you're telling $row to be equal to the first element mysqli will fetch. If it has multiple items, it will just keep on fetching the first item and never stop. Even if it only does have one item, it will keep on fetching because it never gets to the point of $row === false; Commented Jul 13, 2014 at 4:47

1 Answer 1

3

Each time you call while($row = mysqli_fetch_array($MySQL->getReports('jackginger'))) you are making a new query, so it's fetching the samething over and over again.

a solution could be:

<?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>";
}
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.