获取结果后数据库连接的接近程度

时间:2017-05-16 15:51:55

标签: php mysqli

以下代码从数据库中选择用户。我想知道在从数据库中选择数据之后应该使用哪些函数($sql->close(); OR mysqli_free_result($sql);)来关闭连接,以防止出现太多连接错误以及放置它的位置。谢谢

page.php文件

function SelectOneUser ($user) {
    global $connectDb;
    $query = "SELECT * FROM users WHERE name ='".$user."'";
    $sql = mysqli_query($connectDb,$query) or die (mysqli_error());
    if ($sql) {
      $result = array();
      $row = mysqli_fetch_array($sql);
      $result['id'] = $row['id'];
      $result['surname'] = $row['surname'];
      return $result;
    } else {
    return false;
    }
}

连接页面(connection.php)

class sql {
    function connect() {            
        $sqlConnect = mysqli_connect (servername,dbusername,dbpassword,dbname);

        if(!$sqlConnect){
            die("Could not connect to MySQL SERVER");
        }
        return $sqlConnect;
        mysqli_close($sqlConnect);          
    }
}

function.php

    define("servername", "localhost", true);
    define("dbusername", "user", true);
    define("dbpassword", "password", true);

include_once("config.php");
$db = new sql;

global $connectDb;
$connectDb = $db->connect();

include_once("page.php");   
$pages  = new page;

0 个答案:

没有答案