I have my database set up for users, to include username, number, full name, and email. I am trying to make a superuser that has access to the list of users logged into my site. I have been using the prepare SQL: select username from users; and the debug runs through my users yet when I need them to be displayed nothing shows up. I have tried to use a $query that would mess up my code and not show any of the debugs. I have tried the SQL statement in MySQL and it worked. I just don't know why my array is empty. Any help would be great.

<?php
    echo "Debug users.php";
    require "database.php";
    require "session_auth.php";
    $rand = bin2hex(openssl_random_pseudo_bytes(16));
    $_SESSION["nocsrftoken"] = $rand;
    userlist();

    function userlist(){
        echo "<br> Debug function";
        global $mysqli;
        $connection = $mysqli;
        echo "<br> Debug 0";
        $stmt = $connection->prepare('SELECT username FROM users');
        echo "<br> Debug 1";
        $stmt->execute();
        echo "<br> Debug 2";
        if($stmt->fetch()) {
            echo "<br> Debug 3";
            $result = array();
            do {
                echo "<br> Debug 4";
                array_push($result, array( 'username' => $username));
                print_r($result);
                echo $result['username'];
                echo $username;

            } while ($stmt->fetch());

            foreach($result as $r){
                echo "username: ";
                echo $r['username'];
            }
                
        } else {
            //no rows fetched
            echo "No rows fetched";
        }
    }
?> 

I currently have 2 users in my database and the debug runs through both users. This is what my debug code is showing

0

There are 0 best solutions below