【发布时间】:2017-02-17 14:34:29
【问题描述】:
假设我有 2 个 php 文件
在第一个中,我连接到数据库。由于我在另一个 php 文件中需要它,所以我将它分配给这样的全局变量。
setconnection.php
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'database';
// Create connection
$GLOBALS['connect'] = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($GLOBALS['connect']->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$db_selected=mysqli_select_db($GLOBALS['connect'],'database');
mysqli_set_charset($GLOBALS['connect'],'utf8');
?>
然后我有另一个 php 文件,我在其中使用上述 php 文件中的连接。
这样关闭连接是否正确?
index.php
include_once "setconnection.php";
$GLOBALS['connect']->close();
【问题讨论】: