英文:
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
<?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 );
?>
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
<?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.
答案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 "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 );
?>
答案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:
<?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 );
?>
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"; //serverName\instanceName
$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 );
?>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论