0

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Where is `$username` set? Did you forget [to bind this variable](https://www.php.net/manual/en/mysqli-stmt.fetch.php)? – bloodyKnuckles May 01 '22 at 19:42
  • Just trying to make sense of this, I have no variables passing into my user list function. If i wanted to bind the username would i do a statement like this : $stmt->bind_param('s', $username); $stmt->bind_result($username); after my prepared stmt? with $username = $_SESSION["username"]; – epizzah May 01 '22 at 22:21
  • ...like this: `$stmt->bind_param('s', $username);` ?? No. Instructions are here: https://www.php.net/manual/en/mysqli-stmt.fetch.php – bloodyKnuckles May 02 '22 at 01:20
  • just curious, where did you get that code, with if, while, fetch? I cannot remember any tutorial that would tell you anything like that. – Your Common Sense May 02 '22 at 03:51
  • Just in case, a proper guide on mysqli: https://phpdelusions.net/mysqli – Your Common Sense May 02 '22 at 03:52

0 Answers0