user1987641
user1987641

Reputation:

PDO query assistance

So im moving my queries to PDO , and im learning step by step.

I figured buy trial and error, that unlike in normale mysql_ you cannot do a while in a while etc.

so im trying to join tables in a query to get a expected result.

The following query works

$data = $conn->query('SELECT nieuws.id, nieuws.titel, nieuws.intro, nieuws.inhoud,    nieuws.datum, nieuws.auteur, nieuws.categorie, nieuws.tags,nieuws.plaatje, nieuws.plaatje_url,nieuws.mp3,nieuws.mp3_url,nieuws.youtube,nieuws.youtube_url, reacties.id, reacties.nieuwsid, reacties.submitter,reacties.reactie
FROM nieuws 
INNER JOIN reacties
ON nieuws.id = reacties.nieuwsid
ORDER BY reacties.nieuwsid DESC LIMIT 3
');

However, this only returns 1 entry, i want the query to return all "nieuws" entrys, and the corrosponding "reacties" (based upon id)

Translating dutch words to english : Nieuws = news, reacties = comment

So it needs to get id1 with all data for instance and also get nieuwsid1 data. and then do this for all idx results

$data = $conn->query('SELECT * FROM nieuws ORDER BY id DESC limit 3');

gets all the news, howver i do not have any reactions, and since im getting the article etc in the

foreach($data as $row)      

loop i cannot start a second foreach right?

Upvotes: 0

Views: 52

Answers (1)

Greg Bair
Greg Bair

Reputation: 669

Well, you're using an INNER JOIN, which only returns entries from table A that have entries in table B, so if a "News" item doesn't have any comments, it won't be returned.

What you want is a LEFT OUTER JOIN.

Upvotes: 1

Related Questions