none
Can't Connect to SQL server on my local computer using PHP

    Question

  • I wonder if anyone can help me. I'm creating an application in PHP and am using a SQL server database on my local computer to develop the application. The only problem is I cannot connect to the database. My code looks like

    <?php
    $serverName = "LIAMJAY-PC\SQLEXPRESS"; //serverName\instanceName
    
    $connectionInfo = array( "Database"=>"ONEDB");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    
    if( $conn ) {
         echo "Connection established.<br />";
    }else{
         echo "Connection could not be established.<br />";
         die( print_r( sqlsrv_errors(), true));
    }
    ?>

    to connect to the database. However, everytime I run the code I keep getting a 18456 error code. Does anybody know what my problem is and if so, what is the solution???

    Wednesday, August 07, 2013 9:57 AM

Answers

  • Gotcha, since Windows Authentication uses the credentials of the logged in user (or the user context used to run your PHP script), can you check through SSMS if you are able to connect using Windows Authentication to the instance?  I'm trying to help determine if it is a SQL Server connection issue or something with the PHP script.

    Thanks,
    Sam Lester (MSFT)


    http://blogs.msdn.com/b/samlester

    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread.

    Wednesday, August 07, 2013 1:47 PM
    Moderator
  • Hi, what version of SQL Server are you connecting to?  Error 18456 is a login error, so it is likely either an issue with your credentials or your connection string.  Have you tried connecting through SSMS with your same credentials that the php script is running under to validate those credentials have access to your SQL Server instance?  Also, just to confirm, is the SQL Server service running?  

    Thanks,
    Sam Lester (MSFT)




    http://blogs.msdn.com/b/samlester

    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread.

    Wednesday, August 07, 2013 11:30 AM
    Moderator

All replies

  • Hi, what version of SQL Server are you connecting to?  Error 18456 is a login error, so it is likely either an issue with your credentials or your connection string.  Have you tried connecting through SSMS with your same credentials that the php script is running under to validate those credentials have access to your SQL Server instance?  Also, just to confirm, is the SQL Server service running?  

    Thanks,
    Sam Lester (MSFT)




    http://blogs.msdn.com/b/samlester

    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread.

    Wednesday, August 07, 2013 11:30 AM
    Moderator
  • Trying to connect via Windows Authentication, which apparently doesn't use a username and password
    Wednesday, August 07, 2013 12:07 PM
  • Gotcha, since Windows Authentication uses the credentials of the logged in user (or the user context used to run your PHP script), can you check through SSMS if you are able to connect using Windows Authentication to the instance?  I'm trying to help determine if it is a SQL Server connection issue or something with the PHP script.

    Thanks,
    Sam Lester (MSFT)


    http://blogs.msdn.com/b/samlester

    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread.

    Wednesday, August 07, 2013 1:47 PM
    Moderator
  • Hi BigMac,

    I am writing to follow up with you on this post.If you are satisfied with the solution,I'd like to mark this issue as"Answered". Please also feel free to unmark the issue, with any new findings or concerns you may have?


    Thanks
    Sofiya Li


    Sofiya Li
    TechNet Community Support

    Thursday, August 15, 2013 12:58 PM
    Moderator