0

我正在尝试查询五个表。我可以查询其中一张表

$query = "SELECT * FROM Stats_player WHERE player='$user'";

但是,当我尝试使用

$query = "SELECT * FROM Stats_player, Stats_block WHERE player='$user'";

网站中断。这是我用来在屏幕上回显数据的代码

<?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();
?>

关于我如何做到这一点的任何想法?

Stats_player 的表结构:| 柜台 | 播放器 | 游戏时间 |

Stats_block 是:| 柜台 | 播放器 | 块ID |

4

3 回答 3

1
$query = "SELECT * FROM Stats_player, Stats_block WHERE player='$user'"

这很可能是错误的。您应该使用JOIN运算符

在这里,你只是在做两张表的笛卡尔积,这几乎不是你想要的。如果表有很多行,这可能会超出您的资源(内存等)。

就像是

$query = "SELECT * FROM Stats_player p, Stats_block b 
WHERE p.block_id = b.id AND p.player='$user'"

或者

$query = "SELECT * FROM Stats_player p INNER JOIN Stats_block b ON p.block_id = b.id
WHERE p.player='$user'"

或者可能是左外连接......

确切的查询将取决于您的架构。

于 2013-05-22T15:01:13.543 回答
0

你有没有尝试过这样的事情:

SELECT 
table1.field1, table1.field2, table2.field1, table2.field2
FROM
table1, table2
WHERE 
table1.field1 = " " and table2.field1 = " ";
于 2013-05-22T14:59:26.613 回答
0

在没有看到两个表的结构的情况下,这样的事情可能会起作用。

SELECT columnList
FROM Stats_player a
LEFT JOIN Stats_block b ON b.player = a.player
WHERE a.player = '$user'
于 2013-05-22T14:56:46.587 回答