DonOfDen
DonOfDen

Reputation: 4088

PHP Connection with SQL Server 2008 in Windows10

I am trying to connect PHP with SQL Server 2008 but I am unable to establish the connection.

I followed the following tutorials: https://technet.microsoft.com/en-us/library/cc793139(v=sql.90).aspx

My PHP.ini

enter image description here

My Dll and location:

enter image description here

My PHP Version: enter image description here

My Extensions are showing in WAMP PHP Extensions:

enter image description here

But when I try the following PHP code:

<?php
$serverName = "SAPSRV"; //serverName\instanceName

// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.
$connectionInfo = array( "Database"=>"SmartLogistic");
$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 am still getting the following Error:

( ! ) Fatal error: Call to undefined function sqlsrv_connect() in H:\wamp\www\job\sql.php

Help me in Solving the issue, Thanks in Advance..

Upvotes: 0

Views: 2987

Answers (2)

J2D8T
J2D8T

Reputation: 825

I had the exact same error a few months back and here is what worked for me.

First of all for the sqlsrv driver to work you will need 32bit WAMP Server it does not work with 64bit WAMP.

I used the php_sqlsrv_55_ts DLL file which I am able to run my queries within PHP on WAMP.

Now here is where it gets interesting. I am assuming you are going to be running your system on a Linux based server?

If it is the case sqlsrv does not work on Linux based OS so you will have to use the mssql driver on Linux OS. Here is a link to set it up.

Finally you will need to code a custom function which checks your PHP extensions and then depending where you are running them from choose the correct driver.

Here is a function I created to see which drive is loaded and determine which one to use.

// Function to check which MS SQL database driver is loaded
function get_db_loaded_extension() {
    // Assign php loaded extensions to an array variable
    $php_loaded_extensions_array = get_loaded_extensions();

    // Loop through each php extension in the array
    foreach($php_loaded_extensions_array as $php_ext) {
        // Switch to check which MS SQL database driver is loaded
        switch($php_ext) {
            case "mssql":
                $return = "mssql";
                break;

            case "sqlsrv":
                $return = "sqlsrv";
                break;
        }
    }

    // Check if a MS SQL database driver have been found
    if(!isset($return)) {
        $return = js_dialog("No Microsoft SQL database driver loaded.");
    }

    return $return;
}

Just as an example also here is how you can create the connection.

// Call function to check which MS SQL database driver extension is loaded
$mssql_db_driver = get_db_loaded_extension();

// Switch to determine how to make the appropriate connection
switch($mssql_db_driver) {
    case "mssql":
        // Set the MSSQL database variables
        $mssql_servername = "xxx.xxx.xxx.xxx";
        $mssql_username = "my_user";
        $mssql_password = "**********";

        // Create conection to MSSQL using the mssql php extension
        $mssql_conn = mssql_connect($mssql_servername, $mssql_username, $mssql_password);
        break;

    case "sqlsrv":
        // Set the MSSQL database variables
        $mssql_servername = "xxx.xxx.xxx.xxx";
        $mssql_conn_info = array(
          "UID" => "my_user",
          "PWD"=> "**********"
        );

        // Create conection to MSSQL using the sqlsrv php extension
        $mssql_conn = sqlsrv_connect($mssql_servername, $mssql_conn_info);
        break;

    default:
        echo $mssql_db_driver;
        break;
}

// Check the MSSQL connection
if(!$mssql_conn) {
    exit("Could not connect to the database. Please contact the administrator.");
}

Upvotes: 2

Dan Hastings
Dan Hastings

Reputation: 3280

It looks like there is an issue with php rather than sql. sqlsrv_connect is an unknown function so it cannot be called. Try using PDO to start with. Its a nicer and safer way to interact with an SQL database.
http://php.net/manual/en/class.pdo.php

The following should allow you to query a sql server db.

$dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
$stmt = $dbh->prepare("SELECT * FROM Table");
$stmt->execute();

Upvotes: 0

Related Questions