Php 检索所有用户(用户名和用户名)

Php 检索所有用户(用户名和用户名),php,mysql,sql,database,Php,Mysql,Sql,Database,我想检索数据库中的所有用户,并将它们放在一个包含两个元素的数组中;一个包含所有用户id的数组,另一个包含所有用户名的数组。但是,下面的代码不适用于此目的。它所做的是将第一个用户放入数据库中的$field1(第一个用户的用户id)和$field2(第一个用户的用户名) 我需要帮助解决这个问题 基本上,我希望实现以下目标: $userIds = array(1, 2, 3, 4, 5); // The id's from all users in an array $userNames = a

我想检索数据库中的所有用户,并将它们放在一个包含两个元素的数组中;一个包含所有用户id的数组,另一个包含所有用户名的数组。但是,下面的代码不适用于此目的。它所做的是将第一个用户放入数据库中的$field1(第一个用户的用户id)和$field2(第一个用户的用户名)

我需要帮助解决这个问题

基本上,我希望实现以下目标:

$userIds = array(1, 2, 3, 4, 5);    // The id's from all users in an array
$userNames = array("Nisse", "Kalle", "Svenne", "Jonna", "Linda");    // The usernames of all the users in an array

// The arrays with user id's and usernames should be put in a new array that's returned from the function.
$users = array(
  [0] => $userIds,
  [1] => $userNames
);
来自UserHandler.php:

class UserHandler {

    private $m_db = null;

    public function __construct(Database $db) {
        $this->m_db = $db;
    }

    public function GetAllUsers() {

        $userArray = array();

        $query = 'SELECT * FROM Users'; 

        $stmt = $this->m_db->Prepare($query);

        $userArray = $this->m_db->GetUsers($stmt);

        return $userArray;
    }
}
public function GetUsers(\mysqli_stmt $stmt) {
        $userArray = array();

        if ($stmt === FALSE) {
                throw new \Exception($this->mysqli->error);
        }

        //execute the statement
        if ($stmt->execute() == FALSE) {
                throw new \Exception($this->mysqli->error);
        }
        $ret = 0;

        if ($stmt->bind_result($field1, $field2, $field3) == FALSE) {
            throw new \Exception($this->mysqli->error);
        }

        $stmt->fetch();

        $stmt->Close();

        echo $field1 // "1";    <- userid of the first user in db table
        echo $field2 // "Kalle"    <- username of the first user in the db table

        $userArray[] = $field1;    // An array with all the user id's
        $userArray[] = $field2;    // An array with all the usernames

        return $userArray;
}
来自Database.php:

class UserHandler {

    private $m_db = null;

    public function __construct(Database $db) {
        $this->m_db = $db;
    }

    public function GetAllUsers() {

        $userArray = array();

        $query = 'SELECT * FROM Users'; 

        $stmt = $this->m_db->Prepare($query);

        $userArray = $this->m_db->GetUsers($stmt);

        return $userArray;
    }
}
public function GetUsers(\mysqli_stmt $stmt) {
        $userArray = array();

        if ($stmt === FALSE) {
                throw new \Exception($this->mysqli->error);
        }

        //execute the statement
        if ($stmt->execute() == FALSE) {
                throw new \Exception($this->mysqli->error);
        }
        $ret = 0;

        if ($stmt->bind_result($field1, $field2, $field3) == FALSE) {
            throw new \Exception($this->mysqli->error);
        }

        $stmt->fetch();

        $stmt->Close();

        echo $field1 // "1";    <- userid of the first user in db table
        echo $field2 // "Kalle"    <- username of the first user in the db table

        $userArray[] = $field1;    // An array with all the user id's
        $userArray[] = $field2;    // An array with all the usernames

        return $userArray;
}
公共函数GetUsers(\mysqli\u stmt$stmt){
$userArray=array();
如果($stmt==FALSE){
抛出新的\异常($this->mysqli->error);
}
//执行该语句
如果($stmt->execute()==FALSE){
抛出新的\异常($this->mysqli->error);
}
$ret=0;
如果($stmt->bind_result($field1,$field2,$field3)==FALSE){
抛出新的\异常($this->mysqli->error);
}
$stmt->fetch();
$stmt->Close();
echo$field1/“1”仅在从结果表中获取第一行时执行
$stmt->fetch()
,因此需要在结果表上进行循环:

public function GetUsers(\mysqli_stmt $stmt) {
    $userArray = array(
        0 => array(),
        1 => array()
    );

    // the rest of code is the same here

    // replace the line $stmt->fetch() with this block
    while ($stmt->fetch()) {
        array_push($userArray[0], $field1);
        array_push($userArray[1], $field2);
    }

    // remove these lines
    /*
    $userArray[] = $field1;
    $userArray[] = $field2;
    */

    return $userArray
}

$field1
$field2
包含什么?@haynar:它们包含数据库表中第一个用户的第一个用户ID($field1)和用户名($field2)。