Matthias Stähle
Matthias Stähle

Reputation: 97

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

Upvotes: 1

Views: 690

Answers (3)

Matthias Stähle
Matthias Stähle

Reputation: 97

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!

Upvotes: 0

schellingerht
schellingerht

Reputation: 5806

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

Upvotes: 0

Sugumar Venkatesan
Sugumar Venkatesan

Reputation: 4038

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

Upvotes: 2

Related Questions