MySQL abrufen und in HTML table darstellen

MartinMA389

Grünschnabel
Hallo,

ich stehe derzeit vor einem kleinen Problem. Wir fragen per SNMP und OID die Zählerstande der Drucker ab. Diese werden in der Datenbank gespeichert. Nun ist gestern ein SQL-Skript entstanden, welche die Daten sauber aufbereitet aus der Datenbank zieht. Leider bekomme ich die Daten nicht per PHP an ein HTML table übergeben.

Entweder die Seite bleibt weiß, oder es geht nur mit einer bestimmten SELECT Statement. Momentan bin ich mit meinem Latein am Ende und hoffe ihr könnt mir einen Denkanstoß geben.

SQL:
    SELECT distinct 
    t1.host_object_id,
    t1.display_name,
    t1.address,
    CASE WHEN t2.output IS NULL THEN '-' ELSE SUBSTRING(t2.output,11) END as 'Info',
    CASE WHEN t5.output IS NULL THEN '-' ELSE SUBSTRING(t5.output,10) END as 'A3 K',
    CASE WHEN t6.output IS NULL THEN '-' ELSE SUBSTRING(t6.output,10) END as 'A3 C',
    CASE WHEN t7.output IS NULL THEN '-' ELSE SUBSTRING(t7.output,10) END as 'A4 K',
    CASE WHEN t8.output IS NULL THEN '-' ELSE SUBSTRING(t8.output,10) END as 'A4 C',
    CASE WHEN t9.output IS NULL THEN '-' ELSE SUBSTRING(t9.output,10) END as 'SN',
    CASE WHEN t10.output IS NULL THEN '-' ELSE  SUBSTRING(t10.output,10) END as 'POS'


    FROM nagios_hosts t1 LEFT JOIN

                (SELECT distinct t3.host_object_id, t3.service_object_id
                , t3.display_name
                , t4.output
                FROM nagios_services t3
                , nagios_servicestatus t4
                WHERE t3.service_object_id = t4.service_object_id
                and upper(t3.display_name) = upper('Info')
                ) t2 ON t1.host_object_id = t2.host_object_id
                LEFT JOIN
               
                (SELECT distinct t3.host_object_id, t3.service_object_id
                , t3.display_name
                , t4.output
                FROM nagios_services t3
                , nagios_servicestatus t4
                WHERE t3.service_object_id = t4.service_object_id
                and upper(t3.display_name) = Upper('A3 K')
                ) t5 ON t1.host_object_id = t5.host_object_id
                LEFT JOIN
               
                (SELECT distinct t3.host_object_id, t3.service_object_id
                , t3.display_name
                , t4.output
                FROM nagios_services t3
                , nagios_servicestatus t4
                WHERE t3.service_object_id = t4.service_object_id
                and upper(t3.display_name) = upper('A3 C')
                ) t6 ON t1.host_object_id = t6.host_object_id
                LEFT JOIN
               
                (SELECT distinct t3.host_object_id, t3.service_object_id
                , t3.display_name
                , t4.output
                FROM nagios_services t3
                , nagios_servicestatus t4
                WHERE t3.service_object_id = t4.service_object_id
                and upper(t3.display_name) = Upper('A4 K')
                ) t7 ON t1.host_object_id = t7.host_object_id
                LEFT JOIN
               
                (SELECT distinct t3.host_object_id, t3.service_object_id
                , t3.display_name
                , t4.output
                FROM nagios_services t3
                , nagios_servicestatus t4
                WHERE t3.service_object_id = t4.service_object_id
                and upper(t3.display_name) = upper('A4 C')
                ) t8 ON t1.host_object_id = t8.host_object_id
                LEFT JOIN
               
                (SELECT distinct t3.host_object_id, t3.service_object_id
                , t3.display_name
                , t4.output
                FROM nagios_services t3
                , nagios_servicestatus t4
                WHERE t3.service_object_id = t4.service_object_id
                and upper(t3.display_name) = Upper('SN')
                ) t9 ON t1.host_object_id = t9.host_object_id
                LEFT JOIN
               
                (SELECT distinct t3.host_object_id, t3.service_object_id
                , t3.display_name
                , t4.output
                FROM nagios_services t3
                , nagios_servicestatus t4
                WHERE t3.service_object_id = t4.service_object_id
                and upper(t3.display_name) = Upper('POS')
                ) t10 ON t1.host_object_id = t10.host_object_id
               
    WHERE upper( t1.display_name ) LIKE "P-%" ORDER BY t1.host_object_id;

Entweder ein Code mit kompletten SQL-Statement (jetzt ausgeklammert, wäre zu unübersichtlich)
PHP:
<?php
        $host = "";
        $username = "";
        $password = "";
        $dbname = "";   

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "hier das Statement";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr><th>ID</th><th>Name</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "
        <tr>
        <td>".$row["host_object_id"]."</td>
        <td>".$row["display_name"]."</td>
        <td>".$row["address"]."</td>
        <td>".$row["Info"]."</td>
        <td>".$row["A3 K"]."</td>
        <td>".$row["A3 C"]."</td>
        <td>".$row["A4 K"]."</td>
        <td>".$row["A4 C"]."</td>
        <td>".$row["SN"]."</td>
        <td>".$row["POS"]."</td>
        </tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>

Oder ein Prodezurenaufruf (Ja Prozedur ist eingetragen)
HTML:
<!DOCTYPE html>
<html>
    <head>
        <title>Stored Procedure</title>
        <link rel="stylesheet" href="css/table.css" type="text/css" />
    </head>
    <body>
        <?php
        $host = "";
        $username = "";
        $password = "";
        $dbname = "";           
        try {
            $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
            // execute the stored procedure
            $sql = 'CALL spPrinter()';
            // call the stored procedure
            $q = $pdo->query($sql);
            $q->setFetchMode(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            die("Error occurred:" . $e->getMessage());
        }
        ?>
        <table>
            <tr>
                <th>host_object_id</th>
                <th>display_name</th>
            </tr>
            <?php while ($r = $q->fetch()): ?>
                <tr>
                    <td><?php echo $r['host_object_id'] ?></td>
                    <td><?php echo $r['display_name'] ?></td>
                </tr>
            <?php endwhile; ?>
        </table>
    </body>
</html>


Vielen Dank im Voraus.
 
Hi

um beim ersten Code zu bleiben:
Was wird ausgegeben, wenn man nach
Code:
$result = $conn->query($sql);
folgendes einfügt:
Code:
if(!$result)
{
echo $conn->error;
exit(0);
}
und sich den Quelltext der Ausgabe im Browser anschaut (StrgU oder ähnlich, je nach Browser)?
 
Hi

Füg folgendes (nur vorübergehend) der .htaccess-Datei hinzu, vllt. ergibt es einen Unterschied:
Code:
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
 
Hi

Füg folgendes (nur vorübergehend) der .htaccess-Datei hinzu, vllt. ergibt es einen Unterschied:
Code:
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on

Hallo,

jetzt muss ich leider passen.
Ich finde die .htacces Datei nicht. Dazu muss ich gestehen, dass ich das System damals nicht aufgesetzt habe.
Sämtliche Authentifizerung läuft über ein LDAP.
 
So, jetzt habe ich die entsprechenden Tabellen exportiert und bei mir in die Datenbank importiert. Da ich das ganze Konstrukt aktuell noch nicht ganz verstehe, gehe ich diesen Umweg.

Und siehe da, die Seite meldet einen HTTP 500 - Interner Serverfehler.
Möglicherweise enthält die Webseite einen Programmierfehler.
 
.htaccess Datei im Verzeichnis der PHP-Datei angelegt.
Insofern kein Statement eingetragen ist, gibt er sogar 0 Results aus, aber andersrum bleibt die Seite weiterhin weiß.
Entwicklertools bestätigen erneut den Fehler 500.
 
Zurück