Can PHP work with a MS SQL database

82,585

Solution 1

Yes, you can. It depends on which version of PHP you're using, but if you're using PHP5+ you can use Microsoft's SQL Server Driver for PHP. Make sure you use version 2, which gives you the PDO functionality as well as the procedural style.

You can also use the PDO ODBC driver to access a SQL Server instance, but that approach is more buggy and I don't recommend it.

Finally you can use the PHP MSSQL library but that's even worse. Go with Microsoft's own solution if you can.

Edit: Oh, and there's also the DBLIB MSSQL PDO driver - stay away from that one too!

Solution 2

Yes. As long as you have the php_mssql extension on your server, you can use the following common functions:

// Connect to mssql server
$handle = mssql_connect($host, $user, $pass) or die("Cannot connect to server");

// Select a database
$db = mssql_select_db($dn_name, $handle) or die("Cannot select database"); 

// Execute a query
$query = "SELECT * FROM users WHERE lname = 'Smith'";
$result = mssql_query($query);

// Iterate over results<br />
while($row = mssql_fetch_array($result)) {
    echo $row["id"];
}

Note: From PHP 5.3 this extension is not included (and probably not maintained). You can download and add it manually, or better use Microsoft drivers.

Solution 3

Yes, you can use MS SQL and PHP together. Here is just a page from the PHP.net showing all the functions and commands: MS SQL and PHP

It explains everything you need.

Solution 4

For me the solution has been to install the MS drivers as indicated above, and use ADODB library as intermediate. I've had this in production in an intranet over IIS6 and latest MSSQLExpress for months without any issue, perfectly reliable.

Solution 5

yes you can connect to MsSQL . If you are using wamp then switch on the php extension php_mssql if not then use the php.ini file and modify it

Share:
82,585
TH1981
Author by

TH1981

Updated on December 04, 2020

Comments

  • TH1981
    TH1981 over 3 years

    I work primarly with PHP & MySQL, but I have a potential client with a MS SQL and ASP setup. Due to some complicated reasons and offline software integration, they need to keep the databases in the same format, which means not moving to MySQL which would be my personal preference.

    So the question is can I use PHP to access and manipulate an MS SQL database or am I screwed on this one?

    Thanks in advance

  • DForck42
    DForck42 over 13 years
    yup, i'm using the microsoft driver and everything works beautifully.
  • T30
    T30 over 8 years
    Take a look here if you are wondering why MS sqlsrv driver is a better choice.
  • user323094
    user323094 almost 6 years
    Updated link from T30's comment: blogs.msdn.microsoft.com/brian_swan/2010/03/08/…