How to connect sql server with php using xampp?

2019-07-29 12:57发布

问题:

I am trying to connect my SQL server with PHP using Xampp. I have already uploaded dll files in the ext folder but I am unable to connect it. My PHP version is 7.2.6. Uploaded dll files are - php_pdo_sqlsrv_72_ts.dll, php_sqlsrv_72_ts.dll. I have written this code to connect my SQL database with PHP-

<?php   
$serverName = "INDO-SERV\SQLEXPRESS,1443";  
$uid = "sa";     
$pwd = "XXXXXX";    
$databaseName = "web";  
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=>$databaseName);  
$conn = sqlsrv_connect( $serverName, $connectionInfo);  
if( $conn )  
{  
     echo "Connection established.\n";  
}  
else  
{  
     echo "Connection could not be established.\n";  
     die( print_r( sqlsrv_errors(), true));  
}  
sqlsrv_close( $conn);  
?>     
 
 

I am getting this error when I had tried this-

Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect() in C:\xampp\htdocs\biometric\db.php:7 Stack trace: #0 {main} thrown in C:\xampp\htdocs\biometric\db.php on line 7.

Anyone has an idea where I am doing wrong or how to connect with the database.

回答1:

Installation of PHP Driver for SQL Server (sqlsrv and/or pdo_sqlsrv PHP extension) can be done following the next steps:

  • based on Microsoft PHP Drivers for SQL Server Support Matrix download appropriate version of this driver. In your case - version 5.2 or 5.3 (32-bit or 64-bit also depends on PHP version).
  • download and install an appropriate ODBC driver - see System Requirements for the Microsoft Drivers for PHP for SQL Server
  • load PHP Driver for SQL Server as PHP extension.
  • restart Apache

Check the configuration with <?php phpinfo();?>. There should be a section with name pdo_sqlsrv (if you use PDO) and/or sqlsrv (without PDO).



回答2:

my XAMPP version is 7.0.13

1- Download and Install "SQLSRV40.EXE" on this path:

D:\xampp\php\ext

2- Download and Install "msodbcsql.msi"

3- Edit file ":\xampp\php\php.ini" and add this extensions :

extension=php_sqlsrv_7_ts_x86.dll
extension=php_pdo_sqlsrv_7_ts_x86.dll
extension=php7ts.dll

4- restart xampp

Note: "SQLSRV40.EXE" Contain this extensions:
php_sqlsrv_7_ts_x86.dll , php_pdo_sqlsrv_7_ts_x86.dll , php7ts.dll