Omar Alhadidy Omar Alhadidy - 5 months ago 35
MySQL Question

create function in php include while loop

i have this code to call posts from database

<?php
$q = "SELECT * FROM posts WHERE user_id = '$user_info[id]' ORDER BY id DESC";
$r = mysqli_query($dbc, $q);

while($post_info = mysqli_fetch_assoc($r)) { ?>


//html



it works fine but i like to create a function includes the query and the while loop and move it to functions.php and keep the html code in template.php but I don't know how with the while loop..

function data_post($dbc, $user_info['id']){

$q = "SELECT * FROM posts WHERE user_id = '$user_info[id]' ORDER BY id DESC";
$r = mysqli_query($dbc, $q);

while($post_info = mysqli_fetch_assoc($r))

return $post_info
}


I have tried this but there is no result

Answer
function ss()
 {
   ...........

  while($post_info = mysqli_fetch_assoc($r))
  { 
    $new_array[]=$post_info; 

  } 

  return $new_array;

 }

And return the $new_array outside of the while loop

And get the value like this 

  $mm= ss();

 print_r($mm);   here you get that $new_array values