php - Mysqli fetch return just one value

57

i dont know why this function is returning just one value while i have two values in database with id 6 and 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);
}

I have tested the sql in database and it works fine , it returns two values. But when i use this function i get just one value .

Im using this function like that

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

any idea ?

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

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

Answer

Solution:

Your code looks a bit messy. I suggest you trying this simlrier version of your code to check if you're actually getting those two rows you want:

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

People are also looking for solutions to the problem: jquery - Isset get page with php correct usage

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.