我正在尝试通过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,"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;