5

I'm trying to connect from php to Azure DB by

$connectionInfo = array("UID" => "xxx@xxx", "pwd" => "xxx", "Database" => "xxx");
$serverName = "tcp:xxx.database.windows.net,1433";
$conn = sqlsrv_connect($serverName, $connectionInfo);

But it gives me

Fatal error: Call to undefined function sqlsrv_connect() in C:\wamp\www...\index.php on line 19

David Makogon
  • 69,407
  • 21
  • 141
  • 189
Roger
  • 6,443
  • 20
  • 61
  • 88
  • 1
    Why not do a quick Google first and find things like [this](http://msdn.microsoft.com/en-us/library/windowsazure/ff394110.aspx) or [this?](http://blogs.msdn.com/b/brian_swan/archive/2010/02/12/getting-started-with-php-and-sql-azure.aspx) – Pekka Oct 31 '11 at 11:29
  • Ok... thanks. Yet now it says "Fatal error: Call to undefined function sqlsrv_connect() in...", obviously my PHP is missing some extension. – Roger Oct 31 '11 at 11:59
  • 2
    Yup, you need the [SQL server extension](http://www.php.net/manual/en/book.sqlsrv.php). On Windows, it's easy-peasy to add it though, you usually just need to enable the appropriate DLL in php.ini (see the "installation" chapter). – Pekka Oct 31 '11 at 12:05
  • Thanks! Installed it, seems to connect all right! :) Do I work with it as with regular MySql DB from now on? – Roger Oct 31 '11 at 12:16

2 Answers2

8

you have to use the SQL Server native driver for php at first place, then you can do something like:

$serverName = "tcp:sample.database.windows.net, 1433";

$connectionOptions = array("Database" => "sampleInit", 

                           "UID" => "sampleUsr@sample",

                           "PWD" => "samplePass",

                           "MultipleActiveResultSets" => false);

$conn = sqlsrv_connect($serverName, $connectionOptions);

if($conn === false)

{

     die(print_r(sqlsrv_errors(), true));

}

You can read more on PHP and SQL Azure at following blog post:
http://blogs.msdn.com/b/brian_swan/archive/2010/02/12/getting-started-with-php-and-sql-azure.aspx

astaykov
  • 30,768
  • 3
  • 70
  • 86
  • Thanks! Installed it, seems to connect all right! :) Do I work with it as with regular MySql DB from now on? – Roger Oct 31 '11 at 12:17
  • Well, you do work as a regular SQL Server DB from now on:) There are some (may be more) differences between SQL languages in SQL Server and MySQL. For instance in SQL server there is no LIMIT keyword. But in general - yes, from now on you are working as with normal Database. I suggest that you use some sort of DB abstraction layer - such as adodb or any other. – astaykov Oct 31 '11 at 21:26
0

I added this dll to the ext/ folder then added extension=php_sqlsrv.dll to the php.ini in the php7/ folder.

Steve Lloyd
  • 773
  • 2
  • 12
  • 33