Sarah Chaygani Sarah Chaygani - 2 months ago 7
MySQL Question

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

Is it possible to fill an array from the statement->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;
}

Answer

Do you mean like this?

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

In order to echo an ARRAY, you must use print like so:

print_r($member2->showDetails());

That being said, it won't be very... pretty. Use a loop to format how you want:

$users = $member2->showDetails();

foreach ($users as $user) {
    echo $user['User_Name'];
}

Where 'col1' is a column you want to echo.