Bilal Zafar
Bilal Zafar

Reputation: 457

Connect PHP with MS SQL SERVER

I am new to PHP and I am trying to connect my PHP with MS SQL SERVER. I have googled it but not found any good solution.

I am using PHP version : 7.0.6

I have downloaded the required extension and place it in xampp/php/ext folder and added these lines in php.ini file

extension=php_pdo_sqlsrv_7_nts_x64.dll
extension=php_sqlsrv_7_ts_x64.dll
extension=php_pdo_sqlsrv_7_ts_x64.dll
extension=php_sqlsrv_7_nts_x64.dll

and I m using this code to connect to my server.

$myServer = "SERVER_IP"; 
$myUser = "USER_NAME"; 
$myPass = "PASSWORD"; 
$myDB = "DB_NAME"; 

$dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");

bu tit shows me this error:

Fatal error: Uncaught Error: Call to undefined function mssql_connect() in C:\xampp\htdocs\schedule\server.php:2 Stack trace: #0 {main} thrown in C:\xampp\htdocs\schedule\server.php on line 2

Any help in this would be highly appreciated !!

Upvotes: 0

Views: 4205

Answers (3)

Bilal Zafar
Bilal Zafar

Reputation: 457

Issue Solved:

I have used extension=php_odbc.dll

with this code:

$server = '****';
    $user = '****';
    $pass = '****';
    //Define Port
    $port='Port=1433';
    $database = 'cargo_web';

    $connection_string = "DRIVER={SQL Server};SERVER=$server;$port;DATABASE=$database";
    $conn = odbc_connect($connection_string,$user,$pass);
    if ($conn) {
        echo "Connection established.";
    } else{
        die("Connection could not be established.");
    }

Upvotes: 0

Cobuz Alexandru
Cobuz Alexandru

Reputation: 322

You have sqlsrv_connect not mssql_connect, try using this. If it doesn't work, that means you have problems with your extension ( you can also use function_exists to check ).

More info: sqlsrv_connect: http://php.net/manual/ro/function.sqlsrv-connect.php

$serverName = "serverName\sqlexpress"; //serverName\instanceName
$connectionInfo = array( "Database"=>"database_name", "UID"=>"mssql_username", "PWD"=>"mssql_password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

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

Upvotes: 1

Emz
Emz

Reputation: 542

I believe you have to add that mssql extension in order to use that... but you can Use PDO if you want to.. follow the link here

http://php.net/manual/en/ref.pdo-dblib.php

Upvotes: 0

Related Questions