简体   繁体   中英

PHP MySQL - Left Join 3 Tables

i have a logic problem in my PHP/SQL Code.

My script delivers a list of titles. You can view a single title by clicking on it (ajax). In this process, the list is saving in the database.

If you click "back" you should see the previous list again. And here is my problem...

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

The Problem must be here:

$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);

phpMyAdmin的

Thank you

Greets from Germany

I think you specified a wrong column names

$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";

Ok, the error is: "non-object", but you didn't post that code. A right use below:

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

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

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

SOLVED!

ca_begriffe.id, ca_begriffe.title

sorry. that was stupid. I forgot that "ca_". It has been years ago since my last SQL project. Thank you guys!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM