2017-11-30 6 views
0

を返すフェッチ。のMysqliは私がID 6と7で、データベース内の2つの値を持っていながら、この関数はただ一つの値を返している理由を知らないだけで一つの値

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をテストしたところ正常に動作し、2つの値を返します。しかし、私はこの関数を使用するとき、私はちょうど1つの値を取得します。その

$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... 
+0

I)は(ループ$ R->近いながら、あなたが前に、データベース接続を閉じていると思います。 –

+0

@NarendraVermaは、私は外に近い}とその同じ、ただ一つの結果を変更 –

答えて

0

コードが少しばかり見えます。私はあなたが実際にあなたが欲しいものを2つの行を取得しているかどうかを確認するためにあなたのコードのこの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; 
} 
+0

* SQL_CALC_FOUND_ROWS *私が働いて、私はそれをgrabledている必要があります2 –

+0

申し訳返しれる総レコードを取得したいので。 'LIMIT'では' UNION'だけで動作します。投稿したコードを試しましたか? – Azuloo

+0

私は同じことを試みました。上記のコメントの提案も試みました –

関連する問題