是否可以使用fetchAll方法将所有结果提取到数组中?


Is it possible to fetch all the results into an array using fetchAll method

是否可以通过语句->fetchAll()来填充数组?(PDO fetchAll) ?

  public function showDetails()
        {
        $db = ConnectDatabase::getInstance();
        $connect = $db->getConnection();
        $statment = $connect->query('SELECT * FROM users');
        $result = $statment->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

你的意思是这样吗?

$array = $stmt->fetchAll(PDO::FETCH_ASSOC);

为了回显ARRAY,必须像这样使用print:

print_r($member2->showDetails());

话虽如此,它不会很……漂亮。使用循环来设置你想要的格式:

$users = $member2->showDetails();
foreach ($users as $user) {
    echo $user['User_Name'];
}

其中'col1'是要回显的列