+ Reply to Thread
Results 1 to 10 of 10

Thread: Php & mssql

  1. #1
    ziycon's Avatar
    ziycon is offline Wannabe Geek ziycon will become famous soon enough
    Join Date
    Jan 2007
    Location
    Dublin
    Posts
    461
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default [SOLVED]Php & mssql

    I've upgraded to PHP 5.2.6 and im running SQL Server Express 2005. Trying to get a simple query from the SQL server to display in a PHP page, i have the MSSQL module showing up in the phpinfo() page and im running the code below, its not givign any errors just keeps printing out the message i have put if for the 'or die' clause.

    Any ideas or help is always appreciated.

    PHP Code:
    <?
    php  
    'sa';
     = 
    'adm1n';
     = 
    'ADMINSQLEXPRESS';
     = 
    'testdb';

     = 
    mssql_connect (,,) or die('Unable to connect to database.');
    ?>
    Last edited by ziycon; 02-12-2008 at 01:41 AM.

  2. #2
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    did you try the server name as "localhost"
    :. Web Design & Development Web Design Ireland
    :. Search Engines Optimization Search Engines Optimization
    :. Car Parts & Accessories Car Parts
    :. Cars Ireland Cars Ireland
    :. I Have 2 Find It Directory SEF Directory

  3. #3
    Solonox is offline Coder Solonox is on a distinguished road
    Join Date
    Feb 2008
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hey,
    I don't have a lot of experience with PHP these days i am a C# Developer.. However you should be able to do a try / catch with your mssql_connect and then catch the exception. If you could do that and paste the exception here then it will give us much more detail as to what is happening.

    Cheers
    Dave
    Web Design Ireland - CDG Web Design

  4. #4
    ziycon's Avatar
    ziycon is offline Wannabe Geek ziycon will become famous soon enough
    Join Date
    Jan 2007
    Location
    Dublin
    Posts
    461
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by louie View Post
    did you try the server name as "localhost"
    Doesn't make any difference.

    Quote Originally Posted by Solonox View Post
    Hey,
    I don't have a lot of experience with PHP these days i am a C# Developer.. However you should be able to do a try / catch with your mssql_connect and then catch the exception. If you could do that and paste the exception here then it will give us much more detail as to what is happening.

    Cheers
    Dave
    Tried this but unable to figure out how to display the error as if it's a SQL error a try catch in the PHP code wont display the error!?!

  5. #5
    louie's Avatar
    louie is offline Senior Member louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough louie will become famous soon enough
    Join Date
    Jan 2006
    Location
    Dublin, Ireland
    Posts
    2,328
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    try this
    [code
    <?php

    $link = mssql_connect ($servername,$dbusername,$dbpassword) or die(mysql_error().'Unable to connect to database.');
    ?>

    [/code]
    :. Web Design & Development Web Design Ireland
    :. Search Engines Optimization Search Engines Optimization
    :. Car Parts & Accessories Car Parts
    :. Cars Ireland Cars Ireland
    :. I Have 2 Find It Directory SEF Directory

  6. #6
    Solonox is offline Coder Solonox is on a distinguished road
    Join Date
    Feb 2008
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    What Louie has given looks to be exactly what you need to write out the exception. Give that a shot and post up the results till we see what's happening.
    Web Design Ireland - CDG Web Design

  7. #7
    ziycon's Avatar
    ziycon is offline Wannabe Geek ziycon will become famous soon enough
    Join Date
    Jan 2007
    Location
    Dublin
    Posts
    461
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Still only shows up the text i've entered into the 'die' clause, so confused!?!
    Last edited by ziycon; 01-12-2008 at 06:43 PM.

  8. #8
    Solonox is offline Coder Solonox is on a distinguished road
    Join Date
    Feb 2008
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    haha... no i don't think so.

    Can you try this example

    try {
    $hostname = "
    ADMIN\SQLEXPRESS";
    $dbname = "testdb";
    $username = "sa";
    $pw = "adm1n";

    $dbh = new PDO ("mssql:host=$hostname;dbname=$dbname","$username" ,"$pw");
    } catch (PDOException $e) {
    echo "Connection failed because: " . $e->getMessage() . "\n";
    exit;
    }


    That should write out your exception.. However someone of the PHP guys here may have a better way of doing it.. but that should work
    Web Design Ireland - CDG Web Design

  9. #9
    ziycon's Avatar
    ziycon is offline Wannabe Geek ziycon will become famous soon enough
    Join Date
    Jan 2007
    Location
    Dublin
    Posts
    461
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Figured it out after a good few hours, its pretty simple!

    I was using mssql instead of sqlsrv, i'll explain.

    Microsoft released a PHP/SQL 2005 driver for interaction so when this driver/extension is installed and instead of using the mssql_connect() you have to use sqlsrv_connect() as there is a whole new API for the MS driver available here API Reference (SQL Server 2005 Driver for PHP), the old way of connecting to SQL via PHP using the ntwdblib.dll file will not work on a SQL installation after SQL Server 7 as its deprecated, so you need to use SQL Server 2005 Driver for PHP Documentation from now on.

    Thanks for all your help lads!

  10. #10
    Solonox is offline Coder Solonox is on a distinguished road
    Join Date
    Feb 2008
    Posts
    45
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ahh nice one man! Well glad ya got it sorted..
    Web Design Ireland - CDG Web Design

+ Reply to Thread

Visitors found this page by searching for:

Nobody landed on this page from a search engine, yet!
SEO Blog

Tags for this Thread

Posting Permissions

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

Search Engine Optimization by vBSEO

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64