如何使用 PHP 连接到 sql 服务器

2022-08-31 00:31:46

我想使用PHP连接到sql服务器数据库。

我安装了xampp 1.7.0(php 5.2)和SQLSRV20。我已经添加了扩展,但我得到这个错误:php.ini

Warning: mssql_connect() [function.mssql-connect]: Unable to connect to 
server: 10.85.80.229 in C:\xampp\htdocs\xampp\test.php on line 07

法典:

<?php
$myServer = "10.85.80.229";
$myUser = "root";
$myPass = "pass";
$myDB = "testdb";

$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer"); 
?>

此错误消息是什么意思,如何连接到 SQL Server?


答案 1

在 php 中启用 mssql.ini

;extension=php_mssql.dll

extension=php_mssql.dll


答案 2
<?php  
$serverName = "ServerName"; 
$uid = "sqlusername";   
$pwd = "sqlpassword";  
$databaseName = "DBName"; 

$connectionInfo = array( "UID"=>$uid,                            
                         "PWD"=>$pwd,                            
                         "Database"=>$databaseName); 

/* Connect using SQL Server Authentication. */  
$conn = sqlsrv_connect( $serverName, $connectionInfo);  

$tsql = "SELECT id, FirstName, LastName, Email FROM tblContact";  

/* 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));  
}  

/* Iterate through the result set printing a row of data upon each iteration.*/  

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))  
{  
     echo "Col1: ".$row[0]."\n";  
     echo "Col2: ".$row[1]."\n";  
     echo "Col3: ".$row[2]."<br>\n";  
     echo "-----------------<br>\n";  
}  

/* Free statement and connection resources. */  
sqlsrv_free_stmt( $stmt);  
sqlsrv_close( $conn);  
?>  

http://robsphp.blogspot.ae/2012/09/how-to-install-microsofts-sql-server.html


推荐