简体   繁体   中英

store mysql prepare query result in array

I am trying to store mysqli prepare statement result in array. So i can use json encode method and use it in other page.

At the moment it is returning only 1 row. I checked the query and query is working fine.

It seems like i am doing some silly mistake but can't figured it out.

$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))
                                );
                                }

Thanks to Fred. This solved the issue.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM