2

I am fairly new to using PHP. I downloaded XAMPP, and installed everything. PHP 5.5.27 is the version. I ran a test php program which was jsut echo "Hello World". It worked fine. I also was able to connect to MYSQL database using PHP.

$link = mysqli_connect("localhost", "u/n", "pass", "databasename";

Problem i am having and need help is with connecting to sql server. How do i do that? I saw an example online and tried it:

$serverName = "servername";
$connectionInfo = array("Database"="name", "UID"=>"U/N", "PWD"=>"pass";>
$conn = sqlsrv_connect($serverName, $connectionInfo);

But everytime i run this it tells me:

Call to undefined function sqlsrv_connect()

Can someone help me understand what is going on?

chris85
  • 23,846
  • 7
  • 34
  • 51
USER420
  • 337
  • 3
  • 12
  • 3
    I think the SQL server PHP extension is not enabled in XAMPP. – Don't Panic Aug 06 '15 at 20:06
  • 1
    ^^ Check the output from `php -m` to get installed modules. It would appear that you either don't have it installed or not enabled. The `sqlsrv` extension does not ship with PHP. – Michael Berkowski Aug 06 '15 at 20:10
  • 1
    The installation instructions: http://php.net/manual/en/sqlsrv.installation.php – Don't Panic Aug 06 '15 at 20:10
  • 1
    You have to install the sqlsrv extension if it still exists for php 5.5. Alternatives are odbc and pdo_odbc. – Lorenz Meyer Aug 06 '15 at 20:10
  • 2
    I recommend to use PHP's PDO library. – low_rents Aug 06 '15 at 20:13
  • So, I went to http://www.microsoft.com/en-us/download/details.aspx?id=20098 and downloaded SQLSRV32.exe. When i ran the software it asks me to place location where the file are to be extracted, and i extracted them to C:\xampp\php\ext. I restarted my machine and still the same error. Did i do this right, if not what else? – USER420 Aug 06 '15 at 20:45
  • Look at the accepted answer here: http://stackoverflow.com/questions/22831500/connect-sqlsrv-in-xampp – nageeb Aug 06 '15 at 22:18

1 Answers1

1

Consider using PHP's Data Objects (PDO) to connect to SQL Server (in fact you can use it to connect to MySQL or any other database).

Using the MSSQL sqlsrv API (various dlls must be set):

<?php

$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';

try {
    $conn = new PDO("sqlsrv:Server=$server;Database=$database", 
                     $user, $password);
}
catch(PDOException $e) {  
    echo $e->getMessage()."\n";
    exit;
}

?>

Using the ODBC Driver or DSN API (requiring MSSQL ODBC Driver installed which usually ships with database or Windows in general):

<?php

$server = 'servername';
$database = 'databasename';
$username = 'username';
$password = 'pass';

try {
    $dbh = new PDO("odbc:Driver={SQL Server};Server=$server;           
                   database=$database",$username,$password);
}
catch(PDOException $e) {  
    echo $e->getMessage()."\n";
    exit;
}

?>
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Thanks for you response. I ran the bottom example of your code (ODBC driver). ODBC driver is installed. But when i ran your code, it said "could not find driver". How can fix that? – USER420 Aug 07 '15 at 14:32
  • I think this might help in helping me, so i ran phpInfo(), and on the page that opened up I went all the way down to PDO. PDO drivers that were enabled were mysql, and sqlite. How come others are not enabled? – USER420 Aug 07 '15 at 14:45
  • In your php.ini file, uncomment the extension: `php_pdo_odbc.dll`. This might require restarting your web server. – Parfait Aug 07 '15 at 15:50
  • I went a different route. For future is someone else ever comes to this post this is what i did to succed. http://robsphp.blogspot.co.uk/2012/06/unofficial-microsoft-sql-server-driver.html – USER420 Aug 07 '15 at 20:46