0

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 !!

4
  • does phpinfo(); say that you have it installed? Commented Sep 21, 2016 at 6:10
  • @bilal,Can you able to connect mssql in server console. Commented Sep 21, 2016 at 6:11
  • @JapanGuy NO, phpinfo() dont have any portion relating to mssql drivers Commented Sep 21, 2016 at 6:17
  • @Bilal Zafar .. see my answer below.. you can use PDO for that Commented Sep 21, 2016 at 6:18

3 Answers 3

1

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));
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

I have added the extenion in the folder and also in php.ini file
The extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used ---From PHP.NET
0

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.");
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.