[英]How to query MS SQL Server using PHP and not list column names?
I am using PHP to query a Microsoft SQL Server and returning the result set. 我正在使用PHP查询Microsoft SQL Server并返回结果集。 My issue is the PHP code returns the result set that I want, however it is only a partial result set. 我的问题是PHP代码返回我想要的结果集,但是它只是部分结果集。
The full table has 6 columns of data for this example 对于此示例,完整表具有6列数据
Below is the code snippet of the table design in the Database in an HTML Snippet: 以下是HTML代码段中数据库中表设计的代码段:
<!DOCTYPE html> <html> <body> <table border="1" style="width:100%"> <tr> <td>COL1</td> <td>COL2</td> <td>COL3</td> <td>COL4</td> <td>COL5</td> <td>COL6</td> </tr> <tr> <td>rowdata</td> <td>rowdata</td> <td>rowdata</td> <td>rowdata</td> <td>rowdata</td> <td>rowdata</td> </tr> <tr> <td>rowdata</td> <td>rowdata</td> <td>rowdata</td> <td>John</td> <td>rowdata</td> <td>rowdata</td> </tr> </table> </body> </html>
Below is the PHP Script used to query the SQL Server: 以下是用于查询SQL Server的PHP脚本:
<?php
$serverName = "sqlserver";
$uid = "username";
$pwd = "password";
$databaseName = "databasename";
$connectionInfo = array( "UID"=>$uid,
"PWD"=>$pwd,
"Database"=>$databaseName);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$tsql = "SELECT * FROM TABLE " ;
/* Execute the query. */
$stmt = sqlsrv_query( $conn, $tsql);
if ( $stmt )
{
echo "Statement executed.<br>\n";
}
else
{
echo "Error in statement execution.\n";
die( print_r( sqlsrv_errors(), true));
}
//Working Query
/* Iterate through the result set printing a row of data upon each iteration. */
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
foreach ($row as $column=>$value)
{
echo
$column.": " .$value."<br />";
}
echo '<hr />';
}
/* Free statement and connection resources. */
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
This is the issue only 4 columns are returned and 1 row of data for those 4 columns 这对于那些4列的问题,只返回4列和1行数据的
Example of what is returned in PHP Console: PHP控制台返回的示例:
Statement executed.<br>
COL1: rowdata<br />COL2: rowdata<br />COL3: rowdata<br />COL4: rowdata<br />
If you cannot already tell, I am attempting to display the full result set of the query without listing the columns in PHP. 如果您还不能确定,我将尝试显示查询的完整结果集,而不在PHP中列出各列。
IE IE浏览器
echo $row[1];
echo $row[2];
echo $row[3];
etc. 等等
--------------------------------------------------- UPDATE ------------------------------------------------------------ -------------------------------------------------- - 更新 ------------------------------------------------ ------------
I have found there is an issue with the DATETIME Columns in the SQL Server. 我发现SQL Server中的DATETIME列存在问题。 When I CAST() the columns in the SQL Query being passed in the PHP code, it returned the full Result Set. 当我CAST()在PHP代码中传递SQL查询中的列时,它返回了完整的结果集。 This is a work around, however I would like to retain the DATETIME format and just pass a query through instead of Casting it. 这是一种变通方法,但是我想保留DATETIME格式,只是通过查询而不是强制转换。
Thoughts? 有什么想法吗?
UPDATED CODE: 更新的代码:
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionInfo);
$tsql = "SELECT
COL1,
COL2,
COL3,
CAST(COL4 AS VARCHAR) 'COL4'
COL5,
COL6
FROM TABLE " ;
Example of what is returned in PHP Console: PHP控制台返回的示例:
Statement executed.<br>
COL1: rowdata<br />COL2: rowdata<br />COL3: rowdata<br />COL4: rowdata<br />COL5: rowdata<br />COL6: rowdata<br />
COL1: rowdata<br />COL2: rowdata<br />COL3: rowdata<br />COL4: rowdata<br />COL5: rowdata<br />COL6: rowdata<br />
As you can see it returns the entire result set of the table. 如您所见,它返回表的整个结果集。
Your code is trying to read rows as an associate array. 您的代码试图将行作为关联数组读取。 Try using sqlsrv_fetch_assoc instead of sqlsrv_fetch_array as you're doing now. 现在,请尝试使用sqlsrv_fetch_assoc而不是sqlsrv_fetch_array 。
//replace the function call in the line below!
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
foreach ($row as $column=>$value)
{
echo
$column.": " .$value."<br />";
}
echo '<hr />';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.