mysqli::query(): 无法获取 mysqli

2022-08-30 11:26:02

警告:mysqli::query(): 无法在 C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\my portable files\class_EventCalendar.php第 43 行获取 mysqli

以下是我的连接文件:

<?php
if(!isset($_SESSION)) 
{ 
    session_start(); 
}  

// Create array to hold error messages (if any)
$ErrorMsgs = array();

// Create new mysql connection object
$DBConnect = @new mysqli("localhost","root@localhost", 
            NULL,"Ladle");

// Check to see if connection errno data member is not 0 (indicating an error)
if ($DBConnect->connect_errno) {

    // Add error to errors array
    $ErrorMsgs[]="The database server is not available.".
               " Connect Error is ".$DBConnect->connect_errno." ".
               $DBConnect->connect_error.".";
}
?>

这是我的类:

 <?php 
    class EventCalendar {
        private $DBConnect = NULL;

        function __construct() {
            // Include the database connection data
            include("inc_LadleDB.php");
            $this->DBConnect = $DBConnect;  
        }

        function __destruct() {
            if (!$this->DBConnect->connect_error) {
                $this->DBConnect->close();
            }
        }

        function __wakeup() {
            // Include the database connection data
            include("inc_LadleDB.php");     
            $this->DBConnect = $DBConnect;
        }


        // Function to add events to Zodiac calendar
        public function addEvent($Date, $Title, $Description) {
            // Check to see if the required fields of Date and Title have been entered
            if ((!empty($Date)) && (!empty($Title))) {
                /* if all fields are complete then they are 
                   inserted into the Zodiac event_calendar table */
                $SQLString = "INSERT INTO tblSignUps".
                           " (EventDate, Title, Description) ".
                           " VALUES('$Date', '$Title', '".
                            $Description."')";

                // Store query results in a variable
                $QueryResult = $this->DBConnect->query($SQLString);

我对OOP PHP不是很好,我不确定为什么会引发这个错误。我从其他地方提取了这段代码,我唯一更改的是参数。任何人都可以帮助我了解出了什么问题吗?@new mysqli


答案 1

可能在某个地方你有DBconnection->close();然后一些查询试图执行。


提示:在__destruct() 中插入 ...->close(); 有时是错误的(因为 is event ,之后将需要执行查询)__destruct


答案 2

错误的原因是 mysqli 对象的错误初始化。真正的构造是这样的:

$DBConnect = new mysqli("localhost","root","","Ladle");

推荐