简体   繁体   English

查询SQL DB表视图并在HTML表中显示结果

[英]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. 我的目的是在HTML表中显示使用PHP对远程ODBC SQL数据库的查询结果。 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 该环境是Windows上安装的WAMP,PHP 5.6.19,APACHE 2.4.18

This is the code (I have omitted the variables for the odbc connection): 这是代码(我省略了odbc连接的变量):

    <!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). 我想显示表中的所有结果(大约30列和300行)。 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. 我对PHP和MySql相对较新,也许这是一个愚蠢的请求,但暂时无法解决。

Thank you 谢谢

You haven't closed the else bracket for Connection could not be esatblished. 您尚未关闭else支架,因为无法建立Connection。 That's why the Table part is not getting genearted. 这就是为什么Table部分没有被世俗化的原因。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM