[英]Using PHP to get data from multiple database tables
I am trying to query five tables. 我正在尝试查询五个表。 I am able to query one of the tables with
我可以查询其中一个表
$query = "SELECT * FROM Stats_player WHERE player='$user'";
However, when I try to query another table with 但是,当我尝试用
$query = "SELECT * FROM Stats_player, Stats_block WHERE player='$user'";
the website breaks. 网站坏了。 Here is the code I am using to echo the data on the screen
这是我用来在屏幕上回显数据的代码
<?php
if ($result = $mysqli->query($query)) {
echo "<img src=\"https://minotar.net/avatar/{$user}/100\"><h1>{$user}</h1><br/>";
while ($row = $result->fetch_assoc()) {
//variables
$play_time = $row['playtime']/3600;
$play_time = round($play_time, 1);
$xpgained = $row['xpgained'];
$damagetaken = $row['damagetaken'];
$toolsbroken = $row['toolsbroken'];
$itemscrafted = $row['itemscrafted'];
$itemseaten = $row['omnomnom'];
$commandsused = $row['commandsdone'];
$teleports = $row['teleports'];
$itemspickedup = $row['itempickups'];
$itemsdroped = $row['itemdrops'];
$lastseen = date("F j, Y ", strtotime($row['lastjoin']));
//end of variables
echo "<p>Time on Server: {$play_time} HRS</p>";
echo "<p>Last Seen: {$lastseen}";
echo "<p>Commands Used: {$commandsused}";
echo "<p>XP Gained: {$xpgained}";
echo "<p>Blocks broken: {$row['blockID']}"; //this is data from the table Stats_block
}
$result->free();
}
$mysqli->close();
?>
Any ideas on how I might do this? 关于如何执行此操作的任何想法?
Table structor of Stats_player: | Stats_player的表结构: counter |
柜台| player |
播放器 Playtime |
游戏时间
Stats_block is: | Stats_block是: counter |
柜台| player |
播放器 blockID |
blockID |
Without seeing the structure of both tables, something like this could work. 在不看到两个表的结构的情况下,类似的事情可能会起作用。
SELECT columnList
FROM Stats_player a
LEFT JOIN Stats_block b ON b.player = a.player
WHERE a.player = '$user'
Have you tried something like this: 您是否尝试过以下方法:
SELECT
table1.field1, table1.field2, table2.field1, table2.field2
FROM
table1, table2
WHERE
table1.field1 = " " and table2.field1 = " ";
$query = "SELECT * FROM Stats_player, Stats_block WHERE player='$user'"
That's very likely wrong. 这很可能是错误的。 You should use a JOIN operator .
您应该使用JOIN运算子 。
Here, you are just doing a cartesian products of the two tables, that's hardly what you want. 在这里,您只是在对两个表进行笛卡尔乘积运算,这几乎不是您想要的。 And that may overrun your resource (memory etc.) if the tables have a lot of rows.
如果表中有很多行,那可能会浪费您的资源(内存等)。
Something like 就像是
$query = "SELECT * FROM Stats_player p, Stats_block b
WHERE p.block_id = b.id AND p.player='$user'"
or 要么
$query = "SELECT * FROM Stats_player p INNER JOIN Stats_block b ON p.block_id = b.id
WHERE p.player='$user'"
or maybe LEFT OUTER JOIN... 或左外加入...
The exact query will depend on your schema. 确切的查询将取决于您的架构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.