...

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



weronpc
06-11-2004, 08: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

Nightfire
06-11-2004, 09: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 );

weronpc
06-12-2004, 05: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.

Thanx

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

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

newmand2
06-17-2004, 08: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

carl_mcdade
06-17-2004, 09: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");
$db_connection->open($db_connstr);
$rs = $db_connection->execute("SELECT * FROM Table");

$rs->Close();
$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->MoveNext();
}
$res->Close();
$conn->Close();
$res = null;
$conn = null;

odbc connection:

<?php
$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>";
}
odbc_close($dbConnection);
print "</table>";
?>

bcarl314
06-17-2004, 01: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.

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

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

Stored Procedures (now in MySQL but a pia)

Triggers

Microsoft packaging

Exchange Server integration

Clustering

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

stoodder
06-17-2004, 10: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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum