简体   繁体   中英

obtaining comments for each post

hi I have to create a system of comments within various trhead I performed before and all the while the trhead 'while inside the comment and it works but is really slow and with a large number of threads often gives me timeout error how can I fix the problem?

function commenti($id) {

$query2 = "SELECT * FROM table2 WHERE numid='$id' ORDER BY id ASC";

$result2 = mysqli_query($conn,$query2);
if($result2->num_rows >0)
 {

  while($row2 = $result2->fetch_array(MYSQLI_ASSOC))
  {
$idt2 =  $row2['id'];

$testot2 = $row2['testo'];

return $testot2;
  }
 } else {
echo "No comment";

}
}


$query = "SELECT * FROM table1 where visualizza='1' ORDER BY id DESC";

$result = mysqli_query($conn,$query);
if($result->num_rows >0)
 {

  while($row = $result->fetch_array(MYSQLI_ASSOC))
  {
$id =  $row['id'];

$titolo = $row['titolo'];




$testo = commenti($id);
echo "$titolo $testo <br>";
}
}

mysqli_close($conn);
?>

I'm going to assume that you are trying to pull a ton of records. The best way to approach this is to add pagination and only load ~10-20 comments per page. depending on your server

Update:

@OP Basically on first load of the page you load ~10 comments, once they click view more then you load in the next few using ajax. Rinse and repeat.

我以为可以使用联接,但是如果有更多重复项,还可以发表评论

$query = "SELECT * FROM table1 left JOIN table2 ON table1.id = table2.numid where visualizza='1' ORDER BY id DESC";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM