View Full Version : how to: php connect to sql server

06-11-2004, 09:25 AM
I know how to use php to connect to mysql by using mysql_connect(user, pass, host)

How do I use php to connect to other database (eg, MS SQL).

Thank you

06-11-2004, 10:41 AM
$server = "ipaddress:portnumber"; //this is ipaddress-colon-portnumber

$user = "username";

$passwd = "password";

$db_name = "dbanme";

$table_name = "tablename";

$conn = mssql_connect($server, $user, $passwd) or die("Couldn't connect to SQL Server on $server");

mssql_select_db($db_name, $conn);

$query = "SELECT * FROM $table_name ";

$result = mssql_query( $query );

06-12-2004, 06:40 AM
wow, I didn't know php has mssql_connect() method, nice..

how do I find out the port number? I just new to connections.


06-12-2004, 12:51 PM
You could try just using localhost, like with mysql connections. Not sure how you know port numbers

06-17-2004, 07:50 AM
it's nice to have mysql_connect and mssql_connect, what about ms access or other database?

06-17-2004, 09:41 AM
forgive me if I'm wrong, also new to the connections. I have a feeling MS Access is OLEDB, you could start with a search for that

06-17-2004, 10:07 AM
it's nice to have mysql_connect and mssql_connect, what about ms access or other database?

Basically you can use ODBC to connect to any ODBC compliant database (all the good ones) but here are some connections:

To open a DSN-less connection to Access on Win32, you can use this:

$db_connection = new COM("ADODB.Connection");
$db_connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("../databases/database.mdb") . " ;DefaultDir=" . realpath("../databases");
$rs = $db_connection->execute("SELECT * FROM Table");

$db_connection ->Close();
$rs = null;
$db_connection = null;

A network connection:

$db = '\\\\server\\resource\\db.mdb';
$conn = new COM('ADODB.Connection');
$conn->Open("DRIVER={Driver do Microsoft Access (*.mdb)}; DBQ=$db");
$sql = 'SELECT username FROM tblUsuarios';
$res = $conn->Execute($sql);
while (!$res->EOF)
print $res->Fields['username']->Value . "
$res = null;
$conn = null;

odbc connection:

$dbConnection = odbc_connect('accessExample' , '', '');
if (!$dbConnection)
exit("Unable to connect to database: $dbConnection");
$strSQL = "SELECT * FROM search ORDER BY [Category]";
$cur= odbc_exec( $dbConnection, $strSQL);
if (!$cur)
exit("Error in query");
print "<table>";
print "<tr><td><b>Category</b></td><td><b>File</b></td><td><b>Directory</b></td></tr>";
while (odbc_fetch_row($cur))
$category = odbc_result($cur, "Category");
$file = odbc_result($cur, "Page");
$directory = odbc_result($cur, "Directory");
print "<tr><td>$category</td>";
print "<td>$file</td>";
print "<td>$directory</td></tr>";
print "</table>";

06-17-2004, 02:16 PM
I recently ported an app I created in a mysql / linux environment to a mssql / windows enviroment. Changing the code was actually very painless.

Basically boils down to a few steps.

1) replace all instances of "mysql" with "mssql"

2) modify your SQL syntax (mostly things like NOW() -> {fn NOW()} and the like.

3) Test for anything else you may have missed (there is no mssql_last_insert() to get the last insert from a previous query)

All in all it took about 4 hours to migrate a 15,000+ line app.

06-17-2004, 11:16 PM
just out of interest, why could you not just run mysql on the windows server?

06-17-2004, 11:24 PM
Heehe I get this all the time. Reason for using MSSQL over MySQL:

Stored Procedures (now in MySQL but a pia)


Microsoft packaging

Exchange Server integration


blah blah blah blah blah. Boring stuff. There are more reasons some good some bad.

06-17-2004, 11:53 PM
hmm i made database files for a forums sytem never finished lol if you want to take a look at those i could zip them up for you and send them to you if you need them, just ask and ill add an attachment to a post

what they are are 12 files with diffrent functions created bymyself but they all have the same name, the only diffrence is inside they have the type of database changed.. if you know what i mean?