PHP MySQL - 左连接 3 个表

PHP MySQL - Left Join 3 Tables

我的 PHP/SQL 代码有逻辑问题。

我的脚本提供了一个标题列表。您可以通过单击查看单个标题 (ajax)。在此过程中,列表正在保存在数据库中。

如果您单击 "back",您应该会再次看到之前的列表。 这是我的问题...

Table ca_begriff

id  title
2   Giraffe
3   Wetterhahn
4   Eiswürfel
5   Toaster

Table ca_history

id  date
46  1452592732
45  1452592731
44  1452592662

Table ca_history_begriffe

id      history begriff position
263     46      3       1
264     46      9       2
265     46      10      3
266     46      2       4
267     46      4       5

问题一定出在这里:

$sql = "SELECT begriffe.id, begriffe.title FROM ca_begriffe
        LEFT JOIN ca_history_begriffe ON ca_begriffe.id = ca_history_begriffe.begriff
        LEFT JOIN ca_history ON ca_history_begriffe.history = ca_history.id
        WHERE ca_history.id = ".$_GET['r']."
        ORDER BY ca_history_begriffe.position";

$result = $conn->query($sql);

谢谢

来自德国的问候

好的,错误是:"non-object",但您没有 post 该代码。下面一个正确的用法:

$result = $conn->query($sql);

while ($row = $result->fetch_assoc()) {
   // $row is filled!
}

文档:http://php.net/manual/en/mysqli-result.fetch-assoc.php

我认为您指定了错误的列名

$sql = "SELECT ca_begriffe.id, ca_begriffe.title FROM ca_begriffe
    LEFT JOIN ca_history_begriffe ON ca_begriffe.id = ca_history_begriffe.begriff
    LEFT JOIN ca_history ON ca_history_begriffe.history = ca_history.id
    WHERE ca_history.id = ".$_GET['r']."
    ORDER BY ca_history_begriffe.position";

已解决!

ca_begriffe.id, ca_begriffe.title

对不起。那是愚蠢的。我忘了 "ca_"。自从我上一个 SQL 项目以来已经有好几年了。谢谢大家!