使用php和mysql从表中获取所有用户

时间:2016-09-02 11:55:25

标签: php mysql

我正在尝试从用户表中获取所有用户并将响应作为JSON返回,但即使表中有数据,我也会一直显示表的错误。

我的代码:

<?php
include './include/DbHandler.php';

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

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

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

        // looping through result and preparing users array
        while ($user = $result->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);    
?>

getAllUsers功能:

 public function getAllUsers(){

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

    if($stmt->execute()){

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

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

 }

5 个答案:

答案 0 :(得分:0)

试试这个

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

if($stmt->execute()){

    $stmt->store_result();
    if($stmt->num_rows > 0){
        $users = $stmt->get_result();
        $stmt->close();

        return $users;
    } else {
        return NULL;
    }
} else {
    return NULL;
}
getAllUsers函数中的

(在if之前添加$ stmt-&gt; store_result()(stmy_num-rows)

http://php.net/manual/fr/mysqli-stmt.num-rows.php

答案 1 :(得分:0)

尝试使用,

$stmt->fetch();

而不是,

$stmt->get_result();

答案 2 :(得分:0)

另请参阅此链接,这是您在调整后面临的确切问题。只获得2条记录,

Fetch proper data returning only 2 records

答案 3 :(得分:0)

试试这个。这将解决您的问题。

    public function getAllUsers(){

        $stmt = $this->conn->prepare("SELECT u.id, u.first_name, u.last_name, u.mobile, u.token FROM users u");
        if($stmt->execute()){
            $stmt->store_result();
            if($stmt->num_rows > 0){
                $stmt->bind_result($col1, $col2, $col3, $col4, $col5);  
                while ($stmt->fetch()) {
                    printf("%s %s\n", $col1, $col2, $col3, $col4, $col5);
                }
                $stmt->free_result();
                $stmt->close();
            } else {
                return NULL;
            }
        } else {
            return NULL;
        }
}

你必须使用bind_result。请查看我使用此http://php.net/manual/en/mysqli-stmt.bind-result.php

的链接

答案 4 :(得分:0)

类似这样的事情

公共函数getAllUsers(){

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

if($stmt->execute()){

    if($stmt->num_rows > 0){

        if($stmt->get_result() != NULL){
            $response["error"] = false;
            $response["users"] = array();

            // looping through result and preparing users array
            while ($user = $stmt->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";
        }

        $stmt->close();

        return $response;
    } else {
        $stmt->close();
        return NULL;
    }
} else {
    $stmt->close();
    return NULL;
}

}