Mysqli获取只返回一个值

时间:2017-11-30 17:32:31

标签: php mysql arrays mysqli

我不知道为什么这个函数只返回一个值,而我在id为6和7的数据库中有两个值。

 function get_posts(){
     $msg = "";

  if ($countm = $mysqli->prepare("SELECT    SQL_CALC_FOUND_ROWS id,post_userid,name,lang,country,post_image,post_date,post_date_updated FROM posts  ORDER BY `post_date` DESC ;")) {
    $countm->execute(); // Execute the prepared query.
    $countm->store_result();
    $countm->bind_result($id,$post_userid,$name,$lang,$country,$post_image,$post_date,$post_date_updated); // get variables from result.
    $nr = "SELECT FOUND_ROWS() ";
    $r = $mysqli->prepare($nr);
    $r->execute();
     $r->bind_result($no_posts);
     $r->fetch();
     $r->close();
    while ($l[] = $countm->fetch())
    {
       $msg .= ' <tr><td>'. $id .'</td>';
       $msg .= ' <td>'. $post_userid .'</td></tr>';

    } ; 
    $countm->close();
    }else {printf("Prepared Statement Error: %s\n", $mysqli->error);}
return array('msg' => $msg, 'no_posts' => $no_posts);
}

我已经测试了数据库中的sql并且工作正常,它返回两个值。但是当我使用这个功能时,我只得到一个值。

我正在使用这个功能

  $get_posts = get_posts();
  echo $get_posts['msg'];    // returns 1 value

任何想法?

  fetchAll()   fetch_all()  fetch_array() .... all not working im getting this error when using them

  Call to undefined method mysqli_stmt::fetchAll() in...

1 个答案:

答案 0 :(得分:0)

你的代码看起来有点乱。我建议你尝试使用这个simlrier版本的代码来检查你是否真的得到了你想要的那两行:

if ($countm = $mysqli->prepare("SELECT id,post_userid,name,lang,country,post_image,post_date,post_date_updated FROM posts  ORDER BY `post_date` DESC ;")) 
{
    $countm->execute();
    $countm->bind_result($id,$post_userid,$name,$lang,$country,$post_image,$post_date,$post_date_updated);
    while ($countm->fetch()) {
       $msg .= ' <tr><td>'. $id .'</td>';
       $msg .= ' <td>'. $post_userid .'</td></tr>';
    }
    echo $msg;
}