Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to: php connect to sql server

    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

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    PHP Code:
    $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 ); 

  • #3
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    You could try just using localhost, like with mysql connections. Not sure how you know port numbers

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it's nice to have mysql_connect and mssql_connect, what about ms access or other database?

  • #6
    Regular Coder
    Join Date
    Jan 2004
    Location
    London, UK
    Posts
    234
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Dave

  • #7
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by weronpc
    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:
    PHP Code:
    //
    $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:
    PHP Code:
    $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 Code:
    <?php
        $dbConnection 
    odbc_connect('accessExample' ''''); 
        if (!
    $dbConnection
        { 
            exit(
    "Unable to connect to database: $dbConnection"); 
        } 
        
    $strSQL "SELECT * FROM search ORDER BY [Category]"
        
    $curodbc_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>";
    ?>
    Last edited by carl_mcdade; 06-17-2004 at 09:18 AM.
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials

  • #8
    Mega-ultimate member
    Join Date
    Jun 2002
    Location
    Winona, MN - The land of 10,000 lakes
    Posts
    1,855
    Thanks
    1
    Thanked 45 Times in 42 Posts
    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.

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just out of interest, why could you not just run mysql on the windows server?
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #10
    Regular Coder
    Join Date
    May 2004
    Location
    sweden
    Posts
    236
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Carl McDade
    _____________
    Hiveminds Magazine
    for web publisher and community builders
    eRuby Tutorials

  • #11
    Regular Coder
    Join Date
    Oct 2002
    Location
    Milwaukee, Wisconsin
    Posts
    123
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •