1 回答

TA貢獻1827條經驗 獲得超8個贊
只要有可能,您應該避免在循環中使用 SQL,并嘗試一次性獲取所有數據。在這種情況下,您可以JOIN在第一條語句中使用獲取用戶名。就我個人而言,我還會只列出您想要獲取的列,而不是使用*...
$sql = "SELECT t1.id as messageID, t1.from_id, t2.full_name, t2.title
FROM table1 t1
JOIN table2 t2 on t2.id = t1.user_id
WHERE t1.user_id = '$userid' AND t1.unread = 0";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {;
$from = $row["from_id"];
$messageID = $row["messageID"];
?>
<tr>
<td><?php echo $row['full_name'];?></td>
<td><?php echo $row["title"];?></td>
</tr>
<?php
}
}
無法測試它,但應該更有用。
- 1 回答
- 0 關注
- 78 瀏覽
添加回答
舉報