Andrea D'Urso
Andrea D'Urso

Reputation: 1

Query SQL DB Table view and display results in HTML table

my purpose is to display in a HTML table the result of a query to a remote ODBC SQL database with PHP. I've already established the connection with the database, but when i try to execute a query and display it in a table it doesn't show anything. Is there a difference in the query syntax if the table is a view? The environment is WAMP installed on Windows, PHP 5.6.19, APACHE 2.4.18

This is the code (I have omitted the variables for the odbc connection):

    <!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
     border: 1px solid black;
}
</style>
</head>
<body>
<?php


$conn=odbc_connect("Driver={SQL Server};Server=$server;Database=$database;", $user, $password);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( odbc_error(), true));

     $sql = "SELECT * FROM ASSET_VIEW";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr><th>numero</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["N_IMMA"]."</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
}



?>
</body>
</html>

All I get is the "Connection established" string and that's it. I would like to show all the results from the table (about 30 columns and 300 rows). I've tried with different tables but I still get the same result. I'm relatively new to PHP and MySql and maybe it's a silly request but I can't get my head around it for the moment.

Thank you

Upvotes: 0

Views: 1588

Answers (1)

sourabh1024
sourabh1024

Reputation: 657

You haven't closed the else bracket for Connection could not be esatblished. That's why the Table part is not getting genearted.

if( $conn ) {
 echo "Connection established.<br />";
}else{
 echo "Connection could not be established.<br />";
 die( print_r( odbc_error(), true));
}
// Logic to build the table from query

Upvotes: 2

Related Questions