1

Please i am trying to connect to an mssql database on a different machine. Below is my code but i just keep getting a blank page.

I dont know what the issue may be. i have installed php and the mssql drivers.

<?php

    $uid = "******";
    $pwd = "***\$\$****";
    $DB = "***********";
    $serverName = "192.***.**.***";
    $connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=> $DB, "ReturnDatesAsStrings" => true);
    $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));
}

?>
1
  • check with this while making connection: "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password" Commented Aug 4, 2017 at 12:36

2 Answers 2

1

I would suggest that you display the connection error using sqlsrv_errors().

Load the PHP drivers in php.ini file and restart the server.

extension=php_pdo_sqlsrv_54_ts.dll
extension=php_sqlsrv_54_ts.dll

Please see the following example:

<?php
   $serverName = "serverName\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"=>"dbName");
   $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));
   }
?>

For more info: http://php.net/manual/en/function.sqlsrv-connect.php

Sign up to request clarification or add additional context in comments.

Comments

0

This is for Server 2019 and PHP 7.4.

Download SQLSRV 5.8.0 Drivers here

Have this in your php.ini: (depending on hardware 32 vs 64)

extension=pdo_sqlsrv_74_ts_x64
extension=sqlsrv_74_ts_x64

Use this to create a mssql connection:

Function connect_db() {
            $this->connectionInfo = Array("UID" => DB_USER, "PWD" => DB_PASSWORD, "Database" => DB_NAME);
            $this->conn = SQLSRV_CONNECT(DB_SERVER, $this->connectionInfo);
            if ($this->conn){ return; }
            else {echo "Error";die( print_r( sqlsrv_errors(), true ));}
        }

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.