1

Please shame me. What's not right here? I was hoping for something like ->fetch_all(Opt), a one liner, to place all the results in an array but couldn't make it work. This is what I wound up doing:

   $s = "select id, username from users"; 
   $conn = db_connect();
   $sth = $conn->prepare($s);
   $sth->execute();
   $sth->bind_result($id, $un);
   $ida = array();
   while ($sth->fetch()) {
     $ida[] = $id;
   }

I tried

$r = $sth->fetch_all() (tried assigning and not assigning a return value) both using and not using ->bind_result() but both failed. What am I doing wrong?

Kevin
  • 41,694
  • 12
  • 53
  • 70
user116032
  • 321
  • 2
  • 15

1 Answers1

2

First off, make sure that you have mysqlnd on your environment.

Then, to use ->fetch_all(), you'll need to use ->get_result() method first.

Here's the sequence:

$s = "select id, username from users"; 
$conn = db_connect();
$sth = $conn->prepare($s);
$sth->execute();
$data = $sth->get_result(); // get result first
$result = $data->fetch_all(MYSQLI_ASSOC); // then fetch all
print_r($result);
Kevin
  • 41,694
  • 12
  • 53
  • 70