簡體   English   中英

將mysql准備查詢結果存儲在數組中

[英]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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM