We are excited to announce that the IIS.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

Unable to connect database from PHP [Answered]RSS

3 replies

Last post Sep 10, 2015 07:31 AM by Pengzhen Song - MSFT

  • Unable to connect database from PHP

    Sep 03, 2015 06:29 AM|its.mathy|LINK

    Hi there,

    I'm new to webmatrix and PHP.  I'm trying to connect to MySQL database from PHP using the below code.  Couldn't trouble shoot the reason for error.  Can anyone help me to resolve this connection?

    Code:

    <?php
     
        $server=".\SQLEXPRESS";$uid="s2U";$pwd="******";$database="s2";
      
    echo $server, $uid, $pwd ;
     
    $conn = new mysqli($server, $uid, $pwd);
     
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";
     
    ?>

    Output displayed in browser:

    .\SQLEXPRESSs2U******Connection failed: php_network_getaddresses: getaddrinfo failed: No such host is known.

    I got the server details from "Copy connection string" option.

    Thank you.

    databaseconnectionerror

  • Re: Unable to connect database from PHP

    Sep 03, 2015 10:06 PM|Pengzhen Song - MSFT|LINK

    Hi,

    As I know, mysqli can't connect to sql server which is used to access my sql. About sql express, you can use sqlsrv_connect driver:

    $serverName = "serverName\sqlexpress"; //serverName\instanceName
    
    // Since UID and PWD are not specified in the $connectionInfo array,
    // The connection will be attempted using Windows Authentication.
    $connectionInfo = array( "Database"=>"dbName");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    

    For more information, please refer to the document:

    http://php.net/manual/en/function.sqlsrv-connect.php

    databaseconnectionerror

    We are trying to better understand customer views on social support experience. Click HERE to participate the survey.
    Thanks!
  • Re: Unable to connect database from PHP

    Sep 04, 2015 08:16 AM|its.mathy|LINK

    Hi,

    Thanks for your quick response! But unfortunately, it didn't work out for me.  I changed my code like this.

    $serverName = ".\sqlexpress"; //serverName\instanceName
    echo "hi1";
    $connectionInfo = array( "Database"=>"sample", "UID"=>"sampleU", "PWD"=>"******");
    echo "hi3";
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    echo "hi2";
     
    if($conn ) {
         echo "Connection established.<br />";
    }else{
         echo "Connection could not be established.<br />";
         die( print_r( sqlsrv_errors(), true));
    }

    And my output is like this "hi1hi3" which means that the execution died after "$conn = sqlsrv_connect( $serverName, $connectionInfo);".  Could you please help me to resolve this?

    Thank you.

    databaseconnectionerror

  • Re: Unable to connect database from PHP

    Sep 10, 2015 07:31 AM|Pengzhen Song - MSFT|LINK

    Hi,

    Please make sure that you have enabled the extension in php.ini file. And Ensure that the driver is loaded and verify the configuration settings by running a script that calls the phpinfo( ) function.

    For more information, please refer to the document:

    http://www.iis.net/learn/application-frameworks/install-and-configure-php-on-iis/install-the-sql-server-driver-for-php

    And please enable error messages to narrow down the issue:

    http://www.iis.net/learn/application-frameworks/install-and-configure-php-on-iis/improve-php-error-messages-in-iis-7-and-above

    databaseconnectionerror

    We are trying to better understand customer views on social support experience. Click HERE to participate the survey.
    Thanks!