PHP Forum

PHP Forum (http://www.selfphp.de/forum/index.php)
-   MySQLi/PDO/(MySQL) (http://www.selfphp.de/forum/forumdisplay.php?f=22)
-   -   mySQLi join über drei Tabellen (http://www.selfphp.de/forum/showthread.php?t=26030)

Gymno 13.07.2017 13:58:51

mySQLi join über drei Tabellen
 
Hallo ihr!

Ich habe aktuell ein blödes Problem:
Ich habe drei verschiedene Tabellen, welche ich miteinander verbinden muss um sie dann in eine Tabelle via php/html auslesen zu können. Erstmal die drei Tabellen in kleiner Form zur Veranschaulichung:

Inhalt Tabelle icinga_services
host_object_id = 67
service_object_id = 71
display_name = ping4

Inhalt Tabelle icinga_servicestatus
service_object_id = 71
output = PING OK 20ms
last_check = 13.07.2017 12:40:00

Inhalt Tabelle icinga_hosts
host_object_id = 67
alias = icinga.blabla.local

So sieht das ganze immoment aus, der hier ausgeschnittene Code ist Teil einer größeren Datei in der bereits eine andere Datenbank-Abfrage erfolgreich ausgeführt wird. Es sind keine Variablen doppelt vergeben oder Ähnliches, es liegt lediglich an dem verwirrenden SQL-Join... Als Ergebnis kommt beim Ausführen dieses Codes "Ungültige Abfrage", er scheitert also beim Ausführen des SQL Befehls..

Schonmal vielen Dank für eure Hilfe!
PHP-Code:

<?php
    $query 
"    SELECT 
                    icinga_services.host_object_id, 
                    icinga_services.service_object_id,
                    icinga_services.display_name,
                    icinga_servicestatus.service_object_id,
                    icinga_servicestatus.output,
                    icinga_servicestatus.last_check,
                    icinga_hosts.host_object_id,
                    icinga_hosts.alias
                FROM
                    icinga_services
                INNER JOIN
                    icinga_servicestatus
                ON
                    icinga_services.service_output_id = icinga_servicestatus.service_output_id
                INNER JOIN
                    icinga_hosts
                ON
                    icinga_services.host_object_id = icinga_hosts.host_object_id;"
;
        

                
    
$ergebnis2 mysqli_query($link$query);
    if (!
$ergebnis2) {
        die (
'Ungültige Abfrage ' mysqli_error());
    }
    
    
    
    
    
?>
    <div style="margin: 20px 100px 100px 100px; padding: 10px 10px 10px 10px; background-color:#A4A9A9; border-style: solid; width: 800px">
    <?php
        
echo '<table border="1" bordercolor="black" bgcolor="#ffffff">';
              echo 
"<tr>";
                echo 
"<td>&nbsp;&nbsp;&nbsp;&nbsp;<b>HostID</b>&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                echo 
"<td>&nbsp;&nbsp;&nbsp;&nbsp;<b>Hostname</b>&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                echo 
"<td>&nbsp;&nbsp;&nbsp;&nbsp;<b>Service-Name</b>&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                echo 
"<td>&nbsp;&nbsp;&nbsp;&nbsp;<b>Service-Ergebnis</b>&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                echo 
"<td>&nbsp;&nbsp;&nbsp;&nbsp;<b>Zuletzt getestet am:</b>&nbsp;&nbsp;&nbsp;&nbsp;</td>";
            echo 
"</tr>";
        while(
$zeile2 mysqli_fetch_array$ergebnis2)) {
            echo 
"<tr>";
                echo 
"<td>&nbsp;&nbsp;&nbsp;&nbsp;"$zeile2['host_object_id'] . "&nbsp;&nbsp;&nbsp;&nbsp;</td>";  
                 echo 
"<td>&nbsp;&nbsp;&nbsp;&nbsp;"$zeile2['alias'] . "&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                 echo 
"<td>&nbsp;&nbsp;&nbsp;&nbsp;"$zeile2['display_name'] . "&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                 echo 
"<td>&nbsp;&nbsp;&nbsp;&nbsp;"$zeile2['output'] . "&nbsp;&nbsp;&nbsp;&nbsp;</td>";
                 echo 
"<td>&nbsp;&nbsp;&nbsp;&nbsp;"$zeile2['last_check'] . "&nbsp;&nbsp;&nbsp;&nbsp;</td>";
               echo 
"</tr>";
        }
        echo 
"</table></div><br>";
    
?>


//Gymno

Gymno 13.07.2017 14:36:20

AW: mySQL join über drei Tabellen
 
Hat sich erledigt!
Habe das ganze ohne JOINS gelöst. Die haben eh nur verwirrt :D
Funktionierender SQL Befehl:
PHP-Code:

        $query "
                SELECT                     
                    icinga_services.host_object_id, 
                    icinga_services.service_object_id,
                    icinga_services.display_name,
                    icinga_servicestatus.service_object_id,
                    icinga_servicestatus.output,
                    icinga_servicestatus.last_check,
                    icinga_hosts.host_object_id,
                    icinga_hosts.alias 
                FROM 
                    icinga_services, 
                    icinga_hosts, 
                    icinga_servicestatus 
                WHERE 
                    icinga_services.service_object_id = icinga_servicestatus.service_object_id 
                AND 
                    icinga_services.host_object_id = icinga_hosts.host_object_id 
                AND 
                    icinga_hosts.alias LIKE '%rad-az%' 
                AND 
                    icinga_services.display_name LIKE '%ping%'"




Alle Zeitangaben in WEZ +2. Es ist jetzt 13:08:02 Uhr.

Powered by vBulletin® Version 3.8.3 (Deutsch)
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.