PHP sqlsrv 查询到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23131514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP sqlsrv query to database
提问by user3185936
I am migrated from MySQL to MS SQL Server, and trying to fetch all data from the routines table. I am connected but unsure how to fetch data with sqlsrv. This is how far I have came:
我从 MySQL 迁移到 MS SQL Server,并尝试从例程表中获取所有数据。我已连接但不确定如何使用 sqlsrv 获取数据。这是我走了多远:
$conn_array = array (
"UID" => "sa",
"PWD" => "root",
"Database" => "nih_bw",
);
$conn = sqlsrv_connect('BILAL', $conn_array);
if ($conn){
echo "connected";
$result = sqlsrv_query($db->db_conn,"SELECT * FROM routines");
}else{
die(print_r(sqlsrv_errors(), true));
}
sqlsrv_close($conn);
?>
采纳答案by Quijote Shin
First if I'm not wrong you are storing sqlsrv_connect
result into $conn
and this result isn't a class obj its a resource, so remove $db->conn
首先,如果我没记错的话,您将sqlsrv_connect
结果存储 到其中,$conn
并且此结果不是 obj 类,而是资源,因此请删除$db->conn
This example, will connect, then fetch if there are resources returned from sqlsrv_query
这个例子,将连接,然后获取是否有资源返回 sqlsrv_query
$conn_array = array (
"UID" => "sa",
"PWD" => "root",
"Database" => "nih_bw",
);
$conn = sqlsrv_connect('BILAL', $conn_array);
if ($conn){
echo "connected";
if(($result = sqlsrv_query($conn,"SELECT * FROM routines")) !== false){
while( $obj = sqlsrv_fetch_object( $result )) {
echo $obj->colName.'<br />';
}
}
}else{
die(print_r(sqlsrv_errors(), true));
}
回答by Mureinik
After you've successfully executed the query with sqlsrv_query
you can fetch the results, e.g., by using sqlsrv_fetch_array
:
成功执行查询后,sqlsrv_query
您可以获取结果,例如,使用sqlsrv_fetch_array
:
$result = sqlsrv_query($db->db_conn, "SELECT * FROM routines");
if($result === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC) ) {
echo $row['column1'].", ".$row['column2']."<br />";
}
回答by Grzegorz Motyl
Try this:
尝试这个:
while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC) ) {
var_dump($row);
}
sqlsrv_free_stmt($result);