Skip to content
Advertisement

How to fetch all the data in mssql having a multiple server name by using Php And MSSQL

I have module where I need to create a cron job in php, but first thing I need to do is to fetch all the data on the table on every each server name, so now the twist here is we have a lot of server but same database. so is it possible to get all the data in every server name on the same time,

We have 60 Plus server name but I don’t know how can i figure out to set up the connection. For the mean time my setting will be.

Login:sa

Password:1234

Servername: xx.xx.xx.xxx

I already create a sample program but this program the only i can fetch is one server only.

<?php 



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

            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>';


            }


        ?>          

Goal: How to fetch the data in multiple server

Thank you

Advertisement

Answer

Your latest attempt has a simple logical error – you end your foreach loop too early. All the loop does is open each connection. The query happens after the loop has finished, meaning it will only run once, and will only get data from the last database you connected to. Move the end of the loop after all of your code, and you should get the result you expect:

<?php 
    $serverNames = ["xx.xx.xx.xxx\sqlexpress", "xx.xx.xx.xxx\sqlexpress"];//serverNameinstanceName

    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>';
        }
    }
?>
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement