我对 SQL-Server 和 PHP 相当陌生,但我正在尝试使用 PHP 获取表中的总行数。
这是我正在使用的代码,它连接得很好,但它不会打印行数:
<?php
$serverName = "SERVER1\SQLEXPRESS";
$connectionInfo = array( "Database"=>"petitionlist");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
?>
<b>Error: </b>Could not connect to the server database.
<?php
die( print_r( sqlsrv_errors(), true));
}
$tsql = "Select Count(*) from SignatureTable";
$stmt = sqlsrv_query( $conn, $tsql);
if( $stmt === false )
{
?>
<b>Error: </b>
<?php
die( print_r( sqlsrv_errors(), true));
}
?>
<b>Result:</b><br />
<?php
print_r(sqlsrv_fetch_object($stmt));
sqlsrv_free_stmt( $stmt);
sqlsrv_close( $conn);
?>
如果我在 SQL Server Studio 上执行“Select Count(*) from SignatureTable”命令,它会返回正确的数字。所以这不是我的服务器,而是我的代码。
最佳答案
我相信您的来电
print_r(sqlsrv_fetch_object($stmt));
可能是错误的。至少查看有关如何进行调用的文档表明那里可能存在错误。
http://www.php.net/manual/en/function.sqlsrv-fetch-object.php
您可能想尝试使用 fetch 而不是 fetch-object。我建议遵循这个例子。
关于php - 使用 PHP 获取 SQL-Server 中的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12336646/