简体   繁体   English

如何使用 PHP 连接到 SQL 服务器?

[英]How do I connect to SQL server with PHP?

I work with xampp PHP 8.1 I'm trying to connect to SQL Server.我使用 xampp PHP 8.1 我正在尝试连接到 SQL Server。 I downloaded the drivers, but when starting xampp I get the message (in the log file) that the drivers are not found.我下载了驱动程序,但在启动 xampp 时,我收到消息(在日志文件中)说找不到驱动程序。 I have no idea what the problem is, because the extension file path is correct, the drivers are correct, but they won't load.我不知道问题是什么,因为扩展文件路径是正确的,驱动程序是正确的,但它们不会加载。

To connect to a SQL Server database using PHP, you can use the sqlsrv_connect() function.要使用 PHP 连接到 SQL Server 数据库,您可以使用 sqlsrv_connect() 函数。

an exaple:一个例子:

$serverName = "serverName\instanceName";
$databaseName = "databaseName";

$connectionOptions = array(
  "Database" => $databaseName,
  "Uid" => "username",
  "PWD" => "password"
);

$conn = sqlsrv_connect($serverName, $connectionOptions);

if ($conn) {
  // Connection was successful
} else {
  // Connection failed
  die(print_r(sqlsrv_errors(), true));
}

In order to connect with SQL database, you need your given DB username, password and PHP sqlsvr_connect() function.为了连接 SQL 数据库,您需要给定的数据库用户名、密码和 PHP sqlsvr_connect() 函数。

$serverName = "serverName\\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "user_id"=>"your_user_name", "passwod"=>"your_password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection was successful! <br />";
}else{
     echo "Connection was not successful! <br />";
     die( print_r( sqlsrv_errors(), true));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM