如何删除“已建立连接”这一行。从 PHP 到 SQL Server 连接脚本?

问题描述 投票:0回答:2

我使用 XAMPP 通过以下代码在 PHP 和 SQL Server 之间建立了连接:

<?php
$serverName = "192.168.0.6"; //serverName\instanceName
$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 );
?>

这是我运行上面代码时的结果:
(https://i.stack.imgur.com/BcZDZ.jpg)

然后我用下面的代码显示数据,但是消息“已建立连接”。出现。

<?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)

问题是:如何删除消息“已建立连接。”??

我一直在浏览,找不到如何解决这个问题。请帮忙。谢谢。

php connection database-connection
2个回答
0
投票

我猜这段代码在

koneski.php
文件中:

<?php
$serverName = "192.168.0.6"; //serverName\instanceName
$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 );
?>

当您

include
文件时,
koneski.php
文件中的所有内容都包含在内。所以,只需删除这行代码:

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

改成:

// 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));
}

0
投票

a) 仅在未建立连接的情况下打印错误:

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

b) 也在你的其他代码文件中使用

sqlsrv_close( $conn );
,而不是在连接代码文件中。

c) 从您的其他文件中删除

$conn = sqlsrv_connect($serverName, $connectionInfo);
,因为您已经包含了连接代码。

所以代码需要是这样的:

连接一:

<?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));
    }
?>

使用其他文件中的连接:

<?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 );
?>
© www.soinside.com 2019 - 2024. All rights reserved.