Problem solved, many thanks for participation
$StrSQL2 = "SELECT users.benutzername as bn,
kontakt.betreff, kontakt.nachricht
FROM users INNER JOIN kontakt
ON users.userid = kontakt.userid_fk ORDER BY datum DESC";
$abfrage = $db->query($StrSQL2);
echo 'Es wurden '.$abfrage->num_rows.' Nachrichten gefunden!<br>';
while($ausgabe = $abfrage->fetch_object()){
echo '
<b>Datum:</b> '.$ausgabe->datum.' <br>
<b>Betreff:</b> '.$ausgabe->betreff.' <br>
<b>Nachricht:</b><br> '.$ausgabe->nachricht.' <br>
<b>Benutzer:</b><br> '.$ausgabe->bn.'<br><hr>';
}
The code that prints the guestbook entries above uses an INNER JOIN
. INNER JOIN
will only return results that exists on the two table being JOIN
-ed which in this case would be users
and kontakt
. You should use RIGHT JOIN
to allow the kontakt
retrieved even though it do not have users
.
$StrSQL2 = "SELECT users.benutzername as bn,
kontakt.betreff, kontakt.nachricht
FROM users RIGHT JOIN kontakt
ON users.userid = kontakt.userid_fk ORDER BY datum DESC";
The next problem would be your Benutzer
field empty as it originates from the users.benutzername as bn
the user
table. You could modify the code a little bit to show "Anonymous" if the particular value is null. Something like is_null
might worth to try. Your code will be looked like:
while($ausgabe = $abfrage->fetch_object()){
echo '
<b>Datum:</b> '.$ausgabe->datum.' <br>
<b>Betreff:</b> '.$ausgabe->betreff.' <br>
<b>Nachricht:</b><br> '.$ausgabe->nachricht.' <br>
<b>Benutzer:</b><br> '. !is_null($ausgabe->bn) ? $ausgabe->bn : 'Anonymous' .'<br><hr>';
}
The code above uses ternary operator, the ? :
pair.
Sorry, i was late to decide to write a proper answer for the question.