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

48
警告: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);

我对面向对象的PHP不太擅长,也不确定为什么会出现这个错误。我从其他地方复制了这段代码,唯一改变的是@new mysqli的参数。有人能帮我理解出了什么问题吗?
在PHP 8上执行相同的操作时,我发现了一个类似的错误,错误信息如下:

未捕获的错误:mysqli对象已关闭


错误指向的是第43行在哪里? - Marc B
3
尝试修正您的mysqli对象:$DBConnect = @new mysqli("localhost","root","","Ladle"); - Aycan Yaşıt
抱歉...请先尝试上面的方法,您可以访问mysqli对象... - rernesto
3
调用new mysqli()时,为什么要使用@来抑制错误? - Gottlieb Notschnabel
@AycanYaşıt 谢谢,它起作用了,抱歉我不确定如何正确指定空密码或用户字段。 - codingManiac
可能是 警告: mysqli_query():无法获取mysqli 的重复问题。 - Katapofatico
4个回答

112

很可能你有 DBconnection->close();,然后一些查询尝试执行。


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


1
遇到了相同的问题“mysqli :: query():无法获取mysqli”。我在谷歌上搜索并找到了这个答案。我关闭它太晚了。连接和查询数据库后,我包含了一个新的php文件,它不知道连接,所以显示了这个消息。只是评论一下,如果其他人遇到了这个问题。干杯 - Ferdinand Fatal
谢谢,T.T. 教训是:不要允许通过 __destruct() 方法销毁 DB 对象。 - Kevin_Kinsey
@T.Touda 在类构造后__destruct方法没有被触发,但是"The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence."(析构函数会在特定对象没有其他引用时或在关闭序列期间以任何顺序调用。)请参考:https://www.php.net/manual/en/language.oop5.decon.php - Alex
在运行查询之前,请尝试获取新的mysqli实例。 - Jorge Wander Santana Ureña

6

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

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

2
你能解释一下为什么原始代码是“错误”的吗?如果凭据是错误的(这是我看到的唯一更改),那么应该有一个明确的错误消息。 - Nico Haase
@NicoHaase 这很简单,root用户的密码是空字符串,并且提供NULL。当凭据不正确时,错误消息通常如问题顶部所示。 - Aycan Yaşıt
2
@AycanYaşıt,如果提供错误的凭据,您会在哪种情况下收到“无法获取mysqli”的错误消息?此外,文档指出,在不使用参数的情况下,将选择没有密码的用户。 - Nico Haase

5

我曾遇到过同样的问题。我将mysqli对象中的localhost参数更改为'127.0.0.1',而不是写成'localhost'。这样做之后,问题得到了解决;但我不太确定如何以及为什么会起作用。

$db_connection = new mysqli("127.0.0.1","root","","db_name");

-6

检查数据库名称是否不包含“_”或“-”,这对我的情况有所帮助


你能进一步解释一下它是如何连接的吗?如果数据库名称有问题,应该有另一个非常清晰的错误消息,并且允许在数据库名称中使用两种类型的破折号。 - Nico Haase
我无法解释这个,但是通过实验发现,似乎任何破坏命令解释器的破折号都不起作用。如果数据库名称没有破折号-那就完美了。我花了6个小时才第一次遇到这个问题。那些踩负评的人不理解也从未遇到过这个问题,所以他们不知道它是如何工作的,即使看起来很傻。 - Gorodeckij Dimitrij
好的,我还是不明白。在什么情况下会有所区别?在给定的PHP代码中,没有这样的“命令解释器”。 - Nico Haase
PHP是一种解释器,最近与WordPress一起使用。是的,PHP本身没有命名限制,但如果将数据库重命名为短划线,您将收到精确的错误代码。 - Gorodeckij Dimitrij
当您使用带有破折号的数据库名称时,是否收到了“无法获取mysqli”的错误消息?或者您说的是另一种错误消息? - Nico Haase
准确地说,这个错误是:mysqli :: query():无法获取mysqli。我将在本周末安装新的WordPress版本,并检查最新版本的情况。 - Gorodeckij Dimitrij

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接