简体   繁体   中英

connect to SQL Server Database with PHP

I want to connect to a specific database on SQL Server Through PHP. On WAMP Server, I have PHP 5.6 installed so I downloaded SQLSRV32.EXE (Microsoft Drivers for PHP for SQL Server) then I copied these files in C:\\wamp64\\bin\\php\\php5.6.25\\ext 在此处输入图片说明

IN php.ini, I added

extension=php_sqlsrv_56_nts.dll

extension=php_sqlsrv_56_ts.dll

But when i check phpinfo() on browser i don't see the sqlsrv module listed.

When i try to connect to my SQL Server Database I get this error

Fatal error: Call to undefined function sqlsrv_connect()

Here is my code

<?php
$serverName = "PC0CFEP2\SQLEXPRESS"; //serverName\instanceName

// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"test");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

I was checking my solutions on different forums and I tried many but I still can't get it work. I am sure it's in some settings but i couldn't find out what could be the issue.

Any suggestion please ? Thank you very much.

At First, Please run MSSQL server database.

In php.ini file, Please added below code

extension=php_sqlsrv_56_ts.dll
extension=php_pdo_sqlsrv_56_ts.dll

and keep two dll file \\ext folder

To connect database , try

$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

Remove "php_" prefix:

extension=sqlsrv_56_ts.dll
extension=pdo_sqlsrv_56_ts.dll

This worked for me. Thanks for asking this question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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