Sql Server DB connection using php or CakePhp 3.0 in MAMP on windows 7

    1. Download the mssql driver for your php version(in my case it was php 5.6.8) from  here
    2. Extract the .dll files and keep in the  C:\MAMP\bin\php\php5.6.8\ext
    3. Now we have to add the .dll files inside the php configuration file. For this just add the following lines in php.ini which is here   C:\MAMP\conf\php5.6.8
extension=php_pdo_sqlsrv_56_nts.dll
extension=php_sqlsrv_56_nts.dll

extension=php_pdo_sqlsrv_56_ts.dll
extension=php_sqlsrv_56_ts.dll

Connection For php:

<?php 

$serverName = "TOHQLP323\SQLEXPRESS"; //serverName\instanceName
$connectionInfo = array( "Database"=>"TestDb", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if($conn){
    echo 'yeah!!!';
}else{

    echo 'Gosh!!!';
}
?>

Connection For CakePHP:

 'Datasources' => [
        'default' => [
            'className' => 'Cake\Database\Connection',
            'driver' => 'Cake\Database\Driver\Sqlserver',
            'persistent' => false,
            'host' => 'TOHQLP323\SQLEXPRESS',  //'10.12.22.10'
           //'port' => 'nonstandard_port_number',
            'username' => '',
            'password' => '',
            'database' => 'TestDb',
           // 'encoding' => 'utf8',
            'timezone' => 'UTC',
            'cacheMetadata' => true,
            'log' => false,
            'quoteIdentifiers' => false,          
            //'init' => ['SET GLOBAL innodb_stats_on_metadata = 0'],
        ],
 ], 

Now you should be good to go.

Leave a comment