简体   繁体   English

如何使用 PHP 显示 SQL Server 表数据?

[英]How do I display SQL Server table data using PHP?

I'd like to start displaying some data from a sql server database I work with using PHP.我想开始显示我使用 PHP 使用的 sql server 数据库中的一些数据。 I believe my connection to the database is working but I can't get any data from the Facility table to display in the browser.我相信我与数据库的连接工作正常,但我无法从 Facility 表中获取任何数据以显示在浏览器中。 I've been using an Apache server through XAMPP to run PHP(PHP version 8.0).我一直在通过 XAMPP 使用 Apache 服务器来运行 PHP(PHP 8.0 版)。 The SQL server(version 2012) is on another machine in the building. SQL 服务器(2012 版)在大楼内的另一台机器上。 So far I have:到目前为止,我有:

  1. Downloaded the sqlsrv extension files called, "php_sqlsrv_80ts.dll", and "php_sqlsrv_80_ts.dll".下载名为“php_sqlsrv_80ts.dll”和“php_sqlsrv_80_ts.dll”的 sqlsrv 扩展文件。 Both are in my XAMPP php.ini file as new extensions (see below)两者都在我的 XAMPP php.ini 文件中作为新扩展名(见下文) 在此处输入图片说明
  2. Restarted my Apache and MySQL servers after adding the two new extensions.添加两个新扩展后,重新启动了我的 Apache 和 MySQL 服务器。
  3. Tested my connection and tried displaying some results using the code below:测试了我的连接并尝试使用以下代码显示一些结果:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>MSSQL Connection Test</title> </head> <body> <?php $serverName = "###"; //serverName\\instanceName $connectionInfo = array( "Database"=>"UTRBDMSNET", "UID"=>"###", "PWD"=>"###"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo "Connection established.<br />"; }else{ echo "Connection could not be established.<br />"; die( print_r( sqlsrv_errors(), true)); } $query = "SELECT * FROM Facility;"; $result = sqlsrv_query($conn, $query); $resultCheck = sqlsrv_num_rows($result); if ($resultCheck > 0) { while ($row = sqlsrv_fetch_assoc($result)) { echo $row['Operator'] . "<br>"; } } ?> </body> </html>

When I go to the file in my browser I get this message, "Connection established".当我在浏览器中转到该文件时,我收到此消息“已建立连接”。 I don't see anything in the console.我在控制台中没有看到任何内容。 It's hard to tell what's going wrong without any error messages.如果没有任何错误消息,很难判断出了什么问题。 Ideally, I would like to display something from any of the tables in my database to see if things are working.理想情况下,我想从我的数据库中的任何表中显示一些内容,看看事情是否正常。

After looking at the two links @cottton posted, I was able to query some data from the database.查看@cotton 发布的两个链接后,我能够从数据库中查询一些数据。 I found a couple of issues with my code:我发现我的代码有几个问题:

  1. It doesn't seem to like when I use * in my SQL query.当我在 SQL 查询中使用*时似乎不喜欢。 If I specify what column I want to see and tell it to echo the first row in the table, like this, SELECT Operator FROM Facility , it works fine.如果我指定要查看的列并告诉它回显表中的第一行,例如SELECT Operator FROM Facility ,它可以正常工作。 When I say SELECT * FROM Facility and specify the column in the echo statement it gives me this error:当我说SELECT * FROM Facility并在 echo 语句中指定列时,它给了我这个错误:

"Warning: Undefined array key "Operator" in C:\\Users###\\Documents\\GitHub_Repos\\Utah_OG_Website\\MSSQLQueryTest.php on line 58". “警告:C:\\Users###\\Documents\\GitHub_Repos\\Utah_OG_Website\\MSSQLQueryTest.php 第 58 行中未定义的数组键“Operator””。

  1. I needed to use sqlsrv_fetch_array and SQLSRV_FETCH_NUMERIC in the while statement.我需要在 while 语句中使用sqlsrv_fetch_arraySQLSRV_FETCH_NUMERIC NOTE - It only accepts upper-case SQLSRV_FETCH_NUMERIC vs sqlsrv_fetch_numeric .注意 - 它只接受大写SQLSRV_FETCH_NUMERICsqlsrv_fetch_numeric
  2. If I use if ($resultCheck > 0) {} it won't show me any results.如果我使用if ($resultCheck > 0) {}它不会显示任何结果。 I'm assuming it's something related to the type of data being returned, not completely sure yet.我假设这与返回的数据类型有关,但尚不完全确定。

Here's the working piece of code for comparison:这是用于比较的工作代码:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>MSSQL Connection Test</title> </head> <body> <?php $serverName = "###"; //serverName\\instanceName $connectionInfo = array( "Database"=>"###", "UID"=>"###", "PWD"=>"###"); $conn = sqlsrv_connect( $serverName, $connectionInfo); if( $conn ) { echo "Connection established.<br />"; }else{ echo "Connection could not be established.<br />"; die( print_r( sqlsrv_errors(), true)); } $query = "SELECT Operator FROM Facility;"; $result = sqlsrv_query($conn, $query); $resultCheck = sqlsrv_num_rows($result); while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_NUMERIC)) { echo $row[0] . "<br>"; } ?> </body> </html>

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

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