如何从PHP到SQL Server连接脚本中删除”Connection established.”行?

huangapple go评论61阅读模式
英文:

How do I remove the line "Connection established." from the PHP to SQL Server connection script?

问题

我已经用以下代码在XAMPP中建立了PHP与SQL Server之间的连接:

koneksi.php

<?php
$serverName = "192.168.0.6"; // 服务器名\实例名
$connectionInfo = array("Database"=>"goldmart", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn) {
    echo "连接已建立。<br />";
} else {
    echo "无法建立连接。<br />";
    die(print_r(sqlsrv_errors(), true));
}

// 关闭连接。
sqlsrv_close($conn);
?>

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

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

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)

问题是:如何去掉“连接已建立”的消息?

我已经浏览了,但找不到如何解决这个问题。请帮忙。谢谢。

英文:

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

koneksi.php

&lt;?php
$serverName = &quot;192.168.0.6&quot;; //serverName\instanceName
$connectionInfo = array( &quot;Database&quot;=&gt;&quot;goldmart&quot;, &quot;UID&quot;=&gt;&quot;&quot;, &quot;PWD&quot;=&gt;&quot;&quot;);
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo &quot;Connection established.&lt;br /&gt;&quot;;
}else{
     echo &quot;Connection could not be established.&lt;br /&gt;&quot;;
     die( print_r( sqlsrv_errors(), true));
}

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

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

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

tes.php

&lt;?php 
include &quot;koneksi.php&quot;;
$conn = sqlsrv_connect($serverName, $connectionInfo);  
$sql = &quot;SELECT * FROM dbo.mJenis&quot;;
$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[&#39;kode&#39;].&quot;, &quot;.$row[&#39;Nama&#39;].&quot;&lt;br /&gt;&quot;;
}

sqlsrv_free_stmt( $call);
?&gt;

(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.

答案1

得分: 0

a) 只有在连接未建立时打印错误:

if( !$conn ) {
    echo "连接无法建立。<br />";
    die( print_r( sqlsrv_errors(), true));
}

b) 在你的其他代码文件中也使用 sqlsrv_close( $conn );,而不是在连接代码文件中。

c) 从你的其他文件中移除 $conn = sqlsrv_connect($serverName, $connectionInfo);,因为你已经包含了连接代码。

因此,代码应该如下所示:

连接文件 (koneksi.php):

<?php
$serverName = "192.168.0.6";
$connectionInfo = array( "Database"=>"goldmart", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( !$conn ) {
    echo "连接无法建立。<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);
// 关闭连接。
sqlsrv_close( $conn );
?>
英文:

a) Only print the error in case the connection is not established:

if( !$conn ) {
  echo &quot;Connection could not be established.&lt;br /&gt;&quot;;
 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):

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

Another file:

&lt;?php 
	include &quot;koneksi.php&quot;;
	$sql = &quot;SELECT * FROM dbo.mJenis&quot;;
	$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[&#39;kode&#39;].&quot;, &quot;.$row[&#39;Nama&#39;].&quot;&lt;br /&gt;&quot;;
	}

	sqlsrv_free_stmt( $call);
	// Close the connection.
	sqlsrv_close( $conn );
?&gt;

答案2

得分: -1

这段代码似乎在koneski.php文件中:

$serverName = "192.168.0.6"; // 服务器名\实例名
$connectionInfo = array( "Database"=>"goldmart", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "连接已建立。<br />";
}else{
     echo "无法建立连接。<br />";
     die( print_r( sqlsrv_errors(), true));
}

// 关闭连接。
sqlsrv_close( $conn );

当你include这个文件时,会包括在koneski.php文件中的所有内容。所以,只需删除以下代码行:

if( $conn ) {
     echo "连接已建立。<br />";
}else{
     echo "无法建立连接。<br />";
     die( print_r( sqlsrv_errors(), true));
}

将其改成:

// 如果无法建立连接,终止并打印错误
if(!$conn ) {
     echo "无法建立连接。<br />";
     die( print_r( sqlsrv_errors(), true));
}

此外,将SQL关闭操作放到主文件中,而不是koneski.php文件中。

koneski.php:

<?php
$serverName = "192.168.0.6"; // 服务器名\实例名
$connectionInfo = array( "Database"=>"goldmart", "UID"=>"", "PWD"=>"");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if(!$conn ) {
     echo "无法建立连接。<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

Tes.php:

<?php 
include "koneski.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);
// 关闭连接。
sqlsrv_close($conn);
?>
英文:

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

&lt;?php
$serverName = &quot;192.168.0.6&quot;; //serverName\instanceName
$connectionInfo = array( &quot;Database&quot;=&gt;&quot;goldmart&quot;, &quot;UID&quot;=&gt;&quot;&quot;, &quot;PWD&quot;=&gt;&quot;&quot;);
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo &quot;Connection established.&lt;br /&gt;&quot;;
}else{
     echo &quot;Connection could not be established.&lt;br /&gt;&quot;;
     die( print_r( sqlsrv_errors(), true));
}

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

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

if( $conn ) {
     echo &quot;Connection established.&lt;br /&gt;&quot;;
}else{
     echo &quot;Connection could not be established.&lt;br /&gt;&quot;;
     die( print_r( sqlsrv_errors(), true));
}

Change it to:

// If the connection could not be established, die and print the errors
if(!$conn ) {
     echo &quot;Connection could not be established.&lt;br /&gt;&quot;;
     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:

&lt;?php
$serverName = &quot;192.168.0.6&quot;; //serverName\instanceName
$connectionInfo = array( &quot;Database&quot;=&gt;&quot;goldmart&quot;, &quot;UID&quot;=&gt;&quot;&quot;, &quot;PWD&quot;=&gt;&quot;&quot;);
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if(!$conn ) {
     echo &quot;Connection could not be established.&lt;br /&gt;&quot;;
     die( print_r( sqlsrv_errors(), true));
}
?&gt;

Tes.php:

&lt;?php 
include &quot;koneksi.php&quot;;
$conn = sqlsrv_connect($serverName, $connectionInfo);  
$sql = &quot;SELECT * FROM dbo.mJenis&quot;;
$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[&#39;kode&#39;].&quot;, &quot;.$row[&#39;Nama&#39;].&quot;&lt;br /&gt;&quot;;
}

sqlsrv_free_stmt( $call);
// Close the connection.
sqlsrv_close( $conn );
?&gt;

huangapple
  • 本文由 发表于 2023年2月27日 12:38:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定