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

时间:2013-11-12 19:13:00

标签: php mysql mysqli

  

警告:mysqli :: query():无法在第43行的C:\ Program Files(x86)\ EasyPHP-DevServer-13.1VC9 \ data \ localweb \ my portable files \ class_EventCalendar.php中获取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参数。任何人都可以帮我理解出了什么问题吗?

4 个答案:

答案 0 :(得分:69)

您使用 DBconnection->close(); 过早关闭连接! 在你的询问之后做它!


解释:不要在 ...->close(); 中插入 __destruct() ,因为无论何时连接都会立即关闭 CLASS 完成加载。

答案 1 :(得分:8)

错误原因是mysqli对象初始化错误。真正的建设将是这样的:

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

答案 2 :(得分:1)

我遇到了同样的问题。我将mysqli对象中的localhost参数更改为&#39; 127.0.0.1&#39;而不是写#localhost&#39;。有效;我不确定如何或为什么。

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

希望它有所帮助。

答案 3 :(得分:-5)

检查数据库名称是否没有“_”或“ - ” 这有助于我的情况