Home >Backend Development >PHP Tutorial >How to simplify the query in the while loop?
I will post the complete syntax here, please identify it with mysql masters
<code>$gettimeline = mysql_query("SELECT * FROM `timeline` LEFT JOIN `users_profile` USING (id) ORDER BY `addtime_timeline` DESC "); </code>
The above is to find the timeline data table and arrange it by addtime_timeline
<code>while ($row=mysql_fetch_array($gettimeline)){ 印出timeline的欄位資料 } </code>
The above is the cycle for printing timeline data
In the loop I will add query as follows
<code>while ($row=mysql_fetch_array($gettimeline)){ $GETthumb1 = mysql_query("SELECT * FROM `timeline_thumb` WHERE `id` = '".$_SESSION['userid']."' AND `by_timeline_id` = '".$row['timeline_id']."' AND `thumb_type` = '1' "); $pub_GETthumb1 = mysql_query("SELECT * FROM `timeline_thumb` WHERE `by_timeline_id` = '".$row['timeline_id']."' AND `thumb_type` = '1' "); $pub_GETinfo = mysql_query("SELECT * FROM `timeline_thumb` JOIN `users_profile` USING (id) WHERE `by_timeline_id` = '".$row['timeline_id']."' ORDER BY `addtime_thumb` DESC "); $get_comment = mysql_query("SELECT * FROM `timeline_comment` JOIN `users_profile` USING (id) WHERE `re_timeline_id` = '".$row['timeline_id']."' ORDER BY `addtime_comment` DESC "); $get_comment_num = mysql_query("SELECT * FROM `timeline_comment` WHERE `re_timeline_id` = '".$row['timeline_id']."' "); } </code>
GETthumb1 is to find out how many transactions the id (by_timeline_id) matches the timeline (timeline_id) in the timeline_thumb data table (to capture how many transactions the user has).
pub_GETthumb1 shows how many transactions there are in total.
pub_GETinfo is to capture the user's information corresponding to users_profile (id).
get_comment is to capture the number of ids (re_timeline_id) that match timeline (timeline_id) in the timeline_comment data table and capture the user's information corresponding to users_profile (id).
get_comment_num is to get the total number of transactions.
Is there room to simplify mysql queries? ........