2016-04-02 43 views
0

我想在数组中存储mysqli准备语句结果。所以我可以使用json编码方法并在其他页面中使用它。存储mysql准备在数组中的查询结果

此刻它只返回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; 
    } 
+0

为什么不只是做你的查询并将结果推送到数组? – cpugourou

+0

您在每次迭代时覆盖。 '$ data ['name']'只有最后一个名字,所有其他索引也是如此。 – chris85

+0

@ chris85请解释。我没有找到你 – Ironic

回答

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

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

+0

很高兴有帮助,*欢呼声* –

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