1

Im building a function to return an array of arrays in php like this :

 function get_posts(){
        $ids = $post_userids = $names=$langs=$countrys=$post_images=$post_dates= $post_date_updateds = array();
        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(); 
            $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())
    {
      $ids[] = $id ;
      $post_userids[] = $post_userid ; 
      $names[] = $name ; 
      $langs[] = $lang ; 
      $countrys[] = $country ; 
      $post_images[] = $post_image ; 
      $post_dates[] = $post_date ; 
      $post_date_updateds[] = $post_date_updated ;   
     } ; 
       $countm->close();
     }else {printf("Prepared Statement Error: %s\n", $mysqli->error);}
  return array('ids' => $ids,'post_userids' => $post_userids,'names' => $names,'langs' => $langs,'countrys' => $countrys,'post_images' => $post_images,'post_dates' => $post_dates,'post_date_updateds' => $post_date_updateds, 'no_posts' => $no_posts);
  }

Say i have two entries in my table , one with id 6 and other with id 7 and when i try to output

    $get_posts = get_posts();
    echo $get_posts['ids'][0]   //  it output  6

But when i set

   echo $get_posts['ids'][1]   // to get id 7 it output  error 

And this also doesnt work.

    echo $get_posts['no_posts']   //  it output  error  

I dont know if im doing something wrong or i missed something , or if there is better way to achieve this.

Edit :

   var_dump($get_posts)

gives

    array(2) { ["ids"]=> array(1) { [0]=> int(6) } ["no_posts"]=> int(2) }
5
  • 1
    Please var_dump($count_posts) and add the output to your question. Also, your code seems incredibly hard to understand what you're trying to do. Commented Nov 26, 2017 at 22:23
  • There is no count_posts function defined anywhere in code shown. This all looks like a bad science experiment gone wrong Commented Nov 26, 2017 at 22:36
  • @charlietfl , scuzzy sorry , edited Commented Nov 26, 2017 at 22:42
  • @Scuzzy edited with var dumps Commented Nov 26, 2017 at 22:48
  • Why not having just $post[$row['id']] = $row and use fetch_assoc ? Commented Nov 26, 2017 at 22:50

1 Answer 1

1

Try something simple like this:

function get_posts(){
    $posts = array();
    if ($result = $mysqli->query("SELECT  id, post_userid, name, lang, country, post_image, post_date, post_date_updated FROM posts ORDER BY `post_date` DESC ;")) {

        while ($row = $result->fetch_assoc())
        {
            $posts[$row['id']] = $row ;   
        }
    } else {
        printf("Prepared Statement Error: %s\n", $mysqli->error);
    }
    return $posts;
}
Sign up to request clarification or add additional context in comments.

8 Comments

what about other values , names , lange ... ?
Please try and dump $posts all are inside, for id 6, $post[6] will be an associative array, the name is $post[6]['name']
you mean var_dump($get_posts) ? , i couldnt dump posts , its inside function
I cannot understand you can dump inside function, by the way just dumping the result of the function is enough
do i have to give id is 6 ? i dont want give id , because its different every time . i want just fetch every thing ., i have also tried dump inside and didnt work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.