PHP connect to SQL?

Hey guys,

My firm has just finished a site and we are now working on the admin portion of an area of the site. Having initially been told that the site was going to be hosted on a WIN 2003 server running IIS/SQL – which is our area of proficiency – we estimated the project accordingly.

Now the client has told me that the WIN 2003 is the EVENTUAL destination but that they would like the admin and site to run and reside on their current server, which is a Netscape server that would run PHP but still connect to a SQL database, not MySQL.

I didn’t even know that it was possible to connect PHP to SQL. Do you guys have any experience in this? How would I do it?

sure you can, and your probably better off using PHP anyway.

this is a quick example of a database connection include file


<?
$hostname = "localhost";
$username = "username";
$password = "password";
$database = "databasename";

$linkID = mysql_connect($hostname, $username, $password);
mysql_select_db($database, $linkID);
?>

Thanks for the reply!

So I should make this a separate file or include this code into the files of my admin?

Just to clarify, when you say ‘SQL’ you mean ‘Microsoft SQL server’, right? SQL is a language, so you can’t connect to it :wink:

There are a bunch of PHP functions to access MSSQL. They all start with mssql_ and look a lot like the MySQL functions. Start here: http://dk2.php.net/function.mssql-connect

Maybe this article is useful as well: http://www.phpbuilder.com/columns/alberto20000919.php3

o man i’m sorry i didn’t see you said SQL, i thaught you meant mySQL…

I have heard about issues with php -> mssql connections, so you may have to go the ODBC route, which is pretty simple. Just create your database, create a ODBC connector (which on XP and 2k is in the control panel -> Admin Tools -> ODBC connector - should be the same for the 2k3 server) which will give you all the ODBC connector strings.

Some info: http://ca2.php.net/manual/en/function.odbc-connect.php

So if you have trouble connecting directly, try using odbc.