如何将MySQLi预处理语句的所有结果作为关联数组获取?

时间:2017-01-17 10:49:43

标签: php mysql mysqli

如何从MySQLi预处理语句中获取所有结果作为关联数组,而无需一次遍历一行结果?

举个例子:

$results_st = $dbi->prepare("SELECT * FROM foobar");
$results_st->execute();
$results = $results_st->get_results();

// I would like something like:
$results_ar = $results->fetch_all(MYSQLI_ASSOC);

// this should print out all the results in an associate array.
print_r($results_ar);

所需的输出类似于mysqli_fetch_all(MYSQLI_ASSOC)http://php.net/manual/en/mysqli-result.fetch-all.php

Array (
   [0] => (
      'title' => cat,
      'id' => 1
      ),
   [1] => (
      'title' => dog,
      'id' => 2
      ),
   [2] => (
      'title' => bird,
      'id' => 3
      )
);

1 个答案:

答案 0 :(得分:1)

使用fetch_assoc()

$data = $result->fetch_assoc()