Ifwat
Ifwat

Reputation: 1553

MSSQL connection using PHP

Iam so fresh with PHP so this problem so big to me. I dont know why and where my mistake is. Based on my research to connect to MSSQL database by using this code:

<?php
$run = mssql_connect('dev-svr05','sa','P@55w0rd', 'orlig_sm_dev');
if ($run)
{
    echo "Connection OK";
}
else
{
    echo "Connection Failed";
}
?>

But when i run this code i got this error message:

PHP Fatal error: Call to undefined function mssql_connect() in C:\Inetpub\wwwroot\phpscript\save_mssql.php on line 5 

I using the same code to connect to MYSQL and its success but not with MSSQL. Can anybody please tell me why this is happen? Thank You

Upvotes: 1

Views: 728

Answers (3)

Mohit maru
Mohit maru

Reputation: 827

Try "sqlsrv_connect" instead of "mssql_connect"

Upvotes: 0

Tschallacka
Tschallacka

Reputation: 28742

I would advice you download the sql server binaries from windows and use PDO.

try { 
        //In some occasions you only need to define IP/Hostname and you can forgo the \SQLEXPRESS part
        $db = new PDO( "sqlsrv:Server=HOSTNAME\SQLEXPRESS;Database=DATABASENAME","USERNAME","PASSWORD");
       $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
       // set this to your primary database
      $db->query("USE DATABASENAME");
    }

    catch( PDOException $e ) { 

       die( "Error connecting to SQL Server".$e ); 
    }

Upvotes: 1

Sanjay Kumar N S
Sanjay Kumar N S

Reputation: 4749

Fourth parameter is expecting a true/false. Refer the below link and try: http://php.net/manual/en/function.mssql-connect.php

Upvotes: 0

Related Questions