43

警告:mysqli::query(): Couldn't fetch mysqli in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\my便携式文件\class_EventCalendar.php 在第 43 行

以下是我的连接文件:

<?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参数。谁能帮我理解出了什么问题?

4

4 回答 4

105

可能在您拥有的某个地方DBconnection->close();,然后尝试执行一些查询。


提示:插入有时是错误...->close();__destruct()(因为__destruct是事件,之后需要执行查询)

于 2014-10-01T17:48:30.563 回答
7

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

$DBConnect = new mysqli("localhost","root","","Ladle");
于 2013-11-12T19:39:38.783 回答
5

我有同样的问题。我将 mysqli 对象中的 localhost 参数更改为“127.0.0.1”,而不是写“localhost”。有效; 我不确定如何或为什么。

$db_connection = new mysqli("127.0.0.1","root","","db_name");
于 2018-04-29T07:49:38.163 回答
-6

检查数据库名称是否没有对我有帮助的“_”或“-”

于 2017-09-07T18:27:21.643 回答