gpt4 book ai didi

PHP & mysql 显示来自多个表的数据

转载 作者:行者123 更新时间:2023-11-30 22:19:13 25 4
gpt4 key购买 nike

我有两个表,其中包含如下所述的数据:

Table1

TID Name
1 Salman
2 ABC
3 XYZ

表2

SID     STID   SUBJECT
1 1 English
2 1 Math
3 2 Physics
4 2 Math

Table2 STID 中的 Table1 TID 外键。我想从两个表中收集数据并在 PHP 中显示,如下所述:

Name        Subject1    Subject2
Salman English Math
ABC Physic Math

最佳答案

有我的建议算法

$mysqli = new mysqli("HOST", "USER NAME", "USER PASS", "YOUR DATABASE");
$result = $mysqli->query("SELECT * FROM table1");
while ($row = $result->fetch_assoc()){
$TID = $row['TID'];
$Name = $row['Name'];
$result2 = $mysqli->query("SELECT * FROM table2 where `TTID` = $TID");
echo "$Name \t";
while ($row2 = $result2->fetch_assoc()){
$SUBJECT = $row2['SUBJECT'];
echo "$SUBJECT \t";
}
echo '<br/>';
}
$mysqli->close();

关于PHP & mysql 显示来自多个表的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37154455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com