Accessing SQL Servers With PHP
Because of the systems I am using at work now, I need to become adept with Windows servers and databases as well as languages like C#. But I also still support LAMP servers using PHP. I’d like to do most of my development in the new environment, but when I need to get something done quickly, it would be nice to have a PHP environment where I could crank something out in a day. I also may want to port over some existing tools from the LAMP servers to the IIS servers. So, my mission is to get PHP onto the Windows server and make it play nice with the SQL Server.
I have IIS 6 running on a Windows 2003 Server. The database is a SQL 2003 Server. I searched online for a long time to figure out how to make this work, culling through lots of conflicting viewpoints.
MSSQL
A bunch of people mentioned that the ntwdblib.dll that ships with PHP5 is no good; you need to download and replace it with a newer version. I went here http://webzila.com/dll/1/ntwdblib.zip, unzipped it, and placed the file in my c:\php5 directory, overwriting the older version.
You also want to set the system environment variable PHPRC to a value of C:\php5, if that is where you installed PHP.
In addition, you need to make sure that your SQL Server is set to allow mixed mode (both TCP and Named Pipes).
Create yourself a new user in your SQL Server and limit its permissions to readdata and writedata (for now).
In IIS settings, go into the Application Configuration and add a .php extension pointing to your executable path (c:\php\php-cgi.exe or php5isapi.dll). Then, go to the Web Service Extensions and add the same path and give it Allowed permissions.
Restart your server, and then check to see PHP is functioning with phpinfo();.
Paste and edit the following code. If all has gone well, you’ll get some output from your SQL Server.
Note: For SQL Express 2005, change the $server variable to this: $server=”(local)\SQLEXPRESS”;.
<?php
$server=”localhost”;
$username = “yourUsername”;
$password = “yourPassword”;
$sqlconnect=mssql_connect($server, $username, $password);
$sqldb=mssql_select_db(“yourDatabase”,$sqlconnect);
$sqlquery=”SELECT * FROM table_name”;
$results= mssql_query($sqlquery);
while ($row=mssql_fetch_array($results)){
echo $row['row_name'].”<br>\n”;}
mssql_close($sqlconnect);
?>
ODBC
ODBC connections seem faster, though I have no benchmarks, and they are portable. They also appear easier to set up. I simply opened up Data Sources (ODBC) from inside my Control Panel -> Adminstrative Tools and in there opened the System DNS tab. From here I gave my connection a name (php2sql), chose (local) for my Server (although for SQL Express 2005, you’ll want to put ‘machineName\SQLEXPRESS’), input my newly created SQL Server username and password (as above for MSSQL), changed the default from master to my database, and that was it. Again, pasting the code below into a PHP page should display results from your database.
<?php
error_reporting(E_ALL);
$conn=odbc_connect(‘php2sql’,'yourUsername’,'yourPassword’);
if (!$conn)
{exit(“Connection Failed: ” . $conn);}
$sql=”select * from your_table”;
$rs=odbc_exec($conn,$sql);
if (!$rs)
{exit(“Error in SQL”);}
echo “<table><tr>”;
echo “<th>Meta</th>”;
echo “<th>ID</th></tr>”;
while (odbc_fetch_row($rs))
{
$meta=odbc_result($rs,”meta_name”);
$id=odbc_result($rs,”meta_type_id”);
echo “<tr><td>$meta</td>”;
echo “<td>$id</td></tr>”;
}
odbc_close($conn);
echo “</table>”;
?>
I hope that someone finds this helpful and useful. I really had a hard time finding something concise and extensible to make this work. I have these scripts running both in a development environment at work and on both my work and personal laptops.












December 30th, 2006 at 1:44 pm
Ooh, gosh, Chris, sometimes you frighten me. Happy New Year tho — love the LaSallet pics.
July 10th, 2007 at 11:40 am
Hi Chris,
I find this VERY helpful and VERY useful so thank you very much for your effort. It is highly appreciated. Truth is I am also having a hard time finding an answer to a close by and quite simple question: if it is possible to run WordPress blogging platform on the configurations the guys in MY work use: Windows server 2003 and MS SQL 2005… any idea?
Thanks again