php - Replaceing mysqli query in while loop with one query using JOIN -


i read mysql(i) queries used in while loops not performance friendly , i'm going rewrite code because think better. here's question if join can work here. it's single post viewing function should see every comment got posted below. got comment loop in loop post printed.

<?php  $result = (empty($_get['hash'])) ? $_get['hash'] = '' : mysqli_query($conn, "select * `userposts` `hash`='".$_get['hash']."' limit 1");   while($rows = mysqli_fetch_array($result)){   //output of single post     $commentresult = mysqli_query($conn, "select * `comments` `postid`='".$rows['id']."'");     while($commentrows = mysqli_fetch_array($commentresult)){     //output of comments     } } 

so think gets better query join should like:

 $result = (empty($_get['hash'])) ? $_get['hash'] = '' : mysqli_query($conn, "select * `userposts` join comments on userposts.id = comments.postid `hash`='".$_get['hash']."' limit 1"); 

but if i'm trying post data of comments in while loop singlepost printed prints 1 comment. can do?

regards

your code bit of mess. call mysql_fetch_results string, weird.

what want this:

"select * comments left join userposts on comments.postid = userposts.id userposts.hash =  '$_get['hash']'" 

edit: sorry, want left join comments on left. gives comments

edit 2: set , worked expected, multiple posts same hash.

<?php  $hash = $_get['hash']; $result = mysqli_query($conn, "select * comments left join userposts on comments.postid = userposts.id userposts.hash =  '$hash'"); if ( $row = mysql_fetch_array($result)) {     // output of post.     // output first comment } while($row = mysqli_fetch_array($result)){    //output other comments } ?> 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -