简体   繁体   中英

MSSQL DATABASE CONNECTION WITH PHP

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

?>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM