skip to Main Content

I’ve made a connection between PHP and SQL Server using XAMPP with the code below:

koneksi.php

<?php
$serverName = "192.168.0.6"; //serverNameinstanceName
$connectionInfo = array( "Database"=>"goldmart", "UID"=>"", "PWD"=>"");
$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));
}

// Close the connection.
sqlsrv_close( $conn );
?>

This is the result when I run the code above:
(https://i.stack.imgur.com/BcZDZ.jpg)

Then I display the data with the following code, but the message "Connection established." appear.

tes.php

<?php 
include "koneksi.php";
$conn = sqlsrv_connect($serverName, $connectionInfo);  
$sql = "SELECT * FROM dbo.mJenis";
$call=sqlsrv_query($conn, $sql);
if($call === false){
    die(print_r(sqlsrv_errors(), true));
}
while($row = sqlsrv_fetch_array($call, SQLSRV_FETCH_ASSOC)) {
    echo $row['kode'].", ".$row['Nama']."<br />";
}

sqlsrv_free_stmt( $call);
?>

(https://i.stack.imgur.com/Rxs3i.jpg)

The question is: how do I remove the message "Connection established."??

I’ve been browsing and can’t find how to solve this problem. Please help. Thank you.

2

Answers


  1. I’m guessing this code is in the koneski.php file:

    <?php
    $serverName = "192.168.0.6"; //serverNameinstanceName
    $connectionInfo = array( "Database"=>"goldmart", "UID"=>"", "PWD"=>"");
    $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));
    }
    
    // Close the connection.
    sqlsrv_close( $conn );
    ?>
    

    When you include the file, everything inside the koneski.php file is included. So, simply remove this line of code:

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

    Change it to:

    // If the connection could not be established, die and print the errors
    if(!$conn ) {
         echo "Connection could not be established.<br />";
         die( print_r( sqlsrv_errors(), true));
    }
    

    Also, change the SQL close to being in the main file, rather than the koneski.php file.

    koneski.php:

    <?php
    $serverName = "192.168.0.6"; //serverNameinstanceName
    $connectionInfo = array( "Database"=>"goldmart", "UID"=>"", "PWD"=>"");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    
    if(!$conn ) {
         echo "Connection could not be established.<br />";
         die( print_r( sqlsrv_errors(), true));
    }
    ?>
    

    Tes.php:

    <?php 
    include "koneksi.php";
    $conn = sqlsrv_connect($serverName, $connectionInfo);  
    $sql = "SELECT * FROM dbo.mJenis";
    $call=sqlsrv_query($conn, $sql);
    if($call === false){
        die(print_r(sqlsrv_errors(), true));
    }
    while($row = sqlsrv_fetch_array($call, SQLSRV_FETCH_ASSOC)) {
        echo $row['kode'].", ".$row['Nama']."<br />";
    }
    
    sqlsrv_free_stmt( $call);
    // Close the connection.
    sqlsrv_close( $conn );
    ?>
    
    Login or Signup to reply.
  2. a) Only print the error in case the connection is not established:

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

    b) Also use sqlsrv_close( $conn ); in your other code file, not in the connection code file.

    c) remove $conn = sqlsrv_connect($serverName, $connectionInfo); from your other file as you already have the connection code included.

    So the code needs to be like this:

    Connection file (koneksi.php):

    <?php
        $serverName = "192.168.0.6";
        $connectionInfo = array( "Database"=>"goldmart", "UID"=>"", "PWD"=>"");
        $conn = sqlsrv_connect( $serverName, $connectionInfo);
        if( !$conn ) {
             echo "Connection could not be established.<br />";
             die( print_r( sqlsrv_errors(), true));
        }
    ?>
    

    Another file:

    <?php 
        include "koneksi.php";
        $sql = "SELECT * FROM dbo.mJenis";
        $call = sqlsrv_query($conn, $sql);
        if($call === false){
            die(print_r(sqlsrv_errors(), true));
        }
        while($row = sqlsrv_fetch_array($call, SQLSRV_FETCH_ASSOC)) {
            echo $row['kode'].", ".$row['Nama']."<br />";
        }
    
        sqlsrv_free_stmt( $call);
        // Close the connection.
        sqlsrv_close( $conn );
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search