我正在尝试通过php页面从MS SQL中提取数据。我有一个有效的连接,我正在尝试一个简单的SELECT * FROM MyTable,但无论我做什么,sqlsrv_num_rows都是空白的!
这是我的代码:
function connect() {
$serverName = DB_HOST; //serverName\instanceName
$connectionInfo = array( "Database"=>DB_NAME, "UID"=>DB_USER, "PWD"=>DB_PASSWORD);
$this->connection = sqlsrv_connect( $serverName, $connectionInfo);
sqlsrv_connect( $serverName, $connectionInfo);
if( $this->connection ) {
echo "<br>Connection established.<br />";
}else{
echo "<br>Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
}
建立连接的回声是这样的所有AOK!
现在我做一个简单的查询:
function query($sql) {
if ($this->debug) {
echo $sql . "<br />";
}
$this->sql = $sql;
$this->recordset = sqlsrv_query($this->connection, $sql);
if (!$this->recordset) {
die('<br><br>Invalid query :<br><br><bold>' . $this->sql . '</bold><br><br>' . sqlsrv_errors());
}
echo "<br>rows = " . sqlsrv_num_rows($this->recordset);
我从上面的回声中得到了什么?有什么理由吗?或者你能建议一个新的回声我可以尝试调试吗?
我的数据库类中的所有代码都是从mysql转换的,所以可能会有一些错误造成损坏!
我甚至尝试过一个超级简单的版本,所有的代码都在一起,它仍然是空白/错误:
$server = DB_HOST;
$conn = sqlsrv_connect( $server, array( "Database"=>DB_NAME, "UID"=>DB_USER, "PWD"=>DB_PASSWORD) );
$stmt = sqlsrv_query( $conn, "SELECT * FROM MyTable");
$row_count = sqlsrv_num_rows($stmt);
echo "<br>row count = " . $row_count;
if ($row_count === false)
echo "\nerror\n";
else if ($row_count >=0)
echo "\n$row_count\n";
die;
答案 0 :(得分:14)
试试这个......
替换以下声明
sqlsrv_query( $conn, "SELECT * FROM MyTable");
作为
sqlsrv_query( $conn, "SELECT * FROM MyTable", array(), array("Scrollable"=>"buffered"));