gpt4 book ai didi

php 错误发生警告:mysqli::close():无法在 php 中获取 mysqli 错误

转载 作者:行者123 更新时间:2023-11-29 01:28:05 24 4
gpt4 key购买 nike

<分区>

我在编写连接到数据库的代码然后关闭连接时遇到问题。

我的数据库类如下:

class DataBase {
private $conn;
private $info;
private $db;

public function __construct ($servername=DB_SERVER, $user=DB_USER, $pass=DB_PASS, $db=DB_DATABASE) {
$this->db = $db;
$this->conn = new mysqli($servername,$user,$pass, $this->db);


if (!$this->conn)
{
if ($this->conn->connect_error) {
$this->info = "ERROR CODE=DB100: Connection failed <br> " . $conn->connect_error;
$this->close();
echo $this->getInfo();
}
else {
$this->info = "ERROR CODE=DB101: Connection failed <br> ";
$this->close();
echo $this->getInfo();
}
}
}

public function getInfo () {
return $this->info;
}

// send sql to database
public function sendQuery ($sql, $numr=null, $start=null) {

if ($numr) {
if ($start)
$sql .= " LIMIT $start, $numr";
else
$sql .= " LIMIT $numr";
}

$this->conn->set_charset("utf8");
$result = $this->conn->query($sql);

if ($result == true) {
return $result;
}
else {
$this->info = "ERROR CODE=DB102: Sql query not work <br>".$this->conn->error;
$this->close();
echo $this->getInfo();
return false;
}

}

public function sendQueryNoneCahrSet ($sql, $numr=null, $start=null) {
if ($numr) {
if ($start)
$sql .= " LIMIT $start, $numr";
else
$sql .= " LIMIT $numr";
}

$result = $this->conn->query($sql);

if ($result == true) {
return $result;
}
else {
$this->info = "ERROR CODE=DB103: Sql query not work <br>".$this->conn->error;
$this->close();
echo $this->getInfo();
return false;
}
}


// send sql query safe to avoid sql injection.
public function getConn () {
return $this->conn;
}

public function getDataBase (){
return $this->db;
}


// This method close the connection
public function close () {
$this->conn->close();
}

function __destruct() {
$this->close();
}
}

当我运行这段代码时:

           $con = @new DataBase();
$r = $con->sendQuery($sql);
$con->close();

我遇到了这个错误:

Warning: mysqli::close(): Couldn't fetch mysqli in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\projects\automasion\admin\protected\componets\database.php on line 102

程序运行正确并将数据设置到数据库中,但发生此错误。

错误指向:

$this->conn->close();

在我的数据库连接代码中。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com