将mysqlprepare查询结果存储在数组中


store mysql prepare query result in array

我正在尝试将mysqli prepare语句结果存储在数组中。所以我可以使用json编码方法并在其他页面中使用它。

目前它只返回1行。我检查了查询,查询运行良好。

我好像犯了一个愚蠢的错误,但却没能想通。

$start = 2;
$limit = 2;
$result = $mysqli->prepare("SELECT name, email, description, mobile, post_date FROM users order by id DESC limit ?,?");
$result ->bind_param("ii", $start, $limit);
$result->execute();
$result->store_result(); 
$data = array();
$total = $result->num_rows;
if($result->num_rows > 0){ 
    $result->bind_result($name, $email, $description, $mobile, $post_date);

    while ($result->fetch()){
        $data['name'] = $name;
        $data['email'] = $email;
        $data['description'] = $description;
        $data['mobile'] = $mobile;   
        $post_date = $post_date;
        $data['newDate'] = date("d-M-Y", strtotime($post_date));
        //echo $name;
    }
$data = array();
while ($result->fetch()){
                                $data[] = array(
                                    "name" => $name,
                                    "email" => $email,
                                    "description" => $description,
                                    "mobile" => $mobile,
                                    "newDate" => date("d-M-Y", strtotime($post_date))
                                );
                                }

谢谢弗雷德。这解决了问题。

$result->execute();
$data = $result->get_result()->fetch_all(); 
$total = count($data);