SoulAiker
SoulAiker

Reputation: 139

How to fix Connection string is not valid using multiple server name

I have problem regarding implementing multiple server name they told me to put the server name to the array so i workout on it and i make the array look like this. $serverNames = ["xx.xx.xx.xxx\\sqlexpress", "xx.xx.xx.xxx\\sqlexpress"]; then i put it on the implode. $List = implode(', ', $serverName); however the arraying the servernames is not working. it gives me error.

Array ( [0] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][ODBC Driver 17 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. [message] => [Microsoft][ODBC Driver 17 for SQL Server]SQL Server Network Interfaces: Connection string is not valid [87]. ) [1] => Array ( [0] => HYT00 [SQLSTATE] => HYT00 [1] => 0 [code] => 0 [2] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired [message] => [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired ) [2] => Array ( [0] => 08001 [SQLSTATE] => 08001 [1] => 87 [code] => 87 [2] => [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. [message] => [Microsoft][ODBC Driver 17 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online. ) )

To see more my codes that I already created, i will show you my sample.

<?php 



        $serverNames = ["xx.xx.xx.xxx\\sqlexpress", "xx.xx.xx.xxx\\sqlexpress"];//serverName\instanceName

        foreach($serverNames as $ip) {

            $ip_array = $ip;

            $connectionInfo = array( "Database"=>"users", "UID"=>"sa", "PWD"=>"1234");
            $conn = sqlsrv_connect( $ip_array, $connectionInfo);

            if( $conn ) {       
            }else{
                 echo "Connection could not be established.<br />";
                 die( print_r( sqlsrv_errors(), true));
            }
        }

        $tsql = "SELECT * FROM users";
        $stmt = sqlsrv_query($conn,$tsql);

        while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {

                    echo '<tr>';
                        echo '<td style="font-size:11px;">'.$row['Firstname'].'</td>';
                        echo '<td style="font-size:11px;">'.$row['LastName'].'</td>';
                        echo '<td style="font-size:11px;">'.$row['DateBirth'].'</td>';
                    echo '</tr>';


        }


    ?>          

Upvotes: 0

Views: 245

Answers (1)

WEBjuju
WEBjuju

Reputation: 6581

Connect to each server, pull the data rows from that server, or print an error message row if that server is unavailable.

<?php
$serverNames = ['xx.xx.xx.xxx\\sqlexpress', 'xx.xx.xx.xxx\\sqlexpress'];
$connectionInfo = array('Database'=>'users', 'UID'=>'sa', 'PWD'=>'1234');

foreach ($serverNames as $ip) {
  $conn = sqlsrv_connect($ip, $connectionInfo);

  if (empty($conn)) {

    //  data of one server is not extracting to the table
    echo '<tr><td colspan="3">Connection to <strong>Server ';
    echo $ip;
    echo '</strong> could not be established.</td></tr>';
  } else {

    $tsql = 'SELECT * FROM users';
    $stmt = sqlsrv_query($conn, $tsql);

    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {

      echo '<tr>';
      echo '<td style="font-size:11px;">'.$row['Firstname'].'</td>';
      echo '<td style="font-size:11px;">'.$row['LastName'].'</td>';
      echo '<td style="font-size:11px;">'.$row['DateBirth'].'</td>';
      echo '</tr>';
    }
  }
}

Upvotes: 1

Related Questions