get all users from table using php and mysql
<p>I'm trying to get all users from users table and return response as JSON but I'm getting error of table is empty all the time even when there are data on the table.</p>

<p>My code:</p>

<pre><code>&lt;?php
include './include/DbHandler.php';

    $db = new DbHandler();
    $response = array();

    // fetching all users
    $result = $db-&gt;getAllUsers();

    if($result != NULL){
        $response["error"] = false;
        $response["users"] = array();

        // looping through result and preparing users array
        while ($user = $result-&gt;fetch_assoc()) {
            $tmp = array();
            $tmp["user_id"] = $user["id"];
            $tmp["first_name"] = $user["first_name"];
            $tmp["last_name"] = $user["last_name"];
            $tmp["mobile"] = $user["mobile"];
            $tmp["fcm_token"] = $user["token"];
            array_push($response["users"], $tmp);
        }
    } else {
        $response["error"] = true;
        $response["message"] = "No users found on DB";
    }

    echo json_encode($response);    
?&gt;
</code></pre>

<p>The <code>getAllUsers</code> function:</p>

<pre><code> public function getAllUsers(){

    $stmt = $this-&gt;conn-&gt;prepare("SELECT u.id, u.first_name, u.last_name, u.mobile, u.token FROM users u");

    if($stmt-&gt;execute()){

        if($stmt-&gt;num_rows &gt; 0){
            $users = $stmt-&gt;get_result();
            $stmt-&gt;close();

            return $users;
        } else {
            return NULL;
        }
    } else {
        return NULL;
    }

 }
</code></pre>