PHP mysql - Get corresponding user with post -


i'm trying return blog_post,blog_title mysql databse need user , his/her's data (in seperate array's)

the output should like

array(     'blog1' => array(         'title' => 'this title',         'post' => 'this blogs content',         'user' => array(             'username' => 'name',             'lastname' > 'lastname'         )     ),     'blog2' => array(         'title' => 'this title',         'post' => 'this blogs content',         'user' => array(             'username' => 'name',             'lastname' > 'lastname'         )     ) ) 

using mysql there no way of doing in 1 query because mix 2 tables.

i have tried using left_join,right_join , selecting multiple tables like

select * blogs a, users b b.id = a.id 

then have tried using foreach , while loop.

$posts = array(); $x = 0; while(++$x < 20){     $post = db::query('select * posts id = '.$id.' ');     $post['user'] = db::query('select * users id = '.$post['user_id'].' ');      $posts[] = $post; } 

but return same post/user

if need posts particular user possible (assumed have relation in posts table users table foreign key fk_user each post):

$user_id = 1; $posts = db::query(    'select posts.*,    users.username,    users.lastname    posts     join users on users.id = posts.fk_user    user.id = '.$user_id ); 

in case need list of posts related user info can act follows:

$posts = db::query(    'select posts.*, users.username, users.lastname    posts    join users on users.id = posts.fk_user    limit 100' ); // use limit here 

after have $posts array filled can iterate , rearrange (if need) or use is:

foreach( $posts $post ) {    // print post info here or rearrange array    $post['user'] = [       'username' => $post['username'],       'lastname' => $post['lastname'],    ]; } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -