我使用xampp PHP 8.1我正在尝试连接到SQL服务器。我下载了驱动程序,但是当启动xampp时,我得到的消息是(在日志文件中)没有找到驱动程序。我不知道问题是什么,因为扩展文件路径是正确的,驱动程序是正确的,但它们不会加载。
要使用PHP连接SQL Server数据库,可以使用sqlsrv_connect()函数。
的做法:
$serverName = "serverNameinstanceName";
$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));
}
为了连接SQL数据库,您需要给定的数据库用户名,密码和PHP sqlsvr_connect()函数。
$serverName = "serverName\sqlexpress"; //serverNameinstanceName
$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));
}