Ответ 1
Вероятно, вы выполняете запросы после закрытия соединения DBconnection->close();
произошло.
Подсказка: иногда ошибочно вставлять ...->close();
в __destruct()
, потому что __destruct
запускается сразу после построения CLASS.
Предупреждение: mysqli :: query(): не удалось получить mysqli в C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\мои переносимые файлы \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","[email protected]",
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
. Может ли кто-нибудь помочь мне понять, что происходит не так?
Вероятно, вы выполняете запросы после закрытия соединения DBconnection->close();
произошло.
Подсказка: иногда ошибочно вставлять ...->close();
в __destruct()
, потому что __destruct
запускается сразу после построения CLASS.
Причина ошибки - неправильная инициализация объекта mysqli. Истинное построение было бы таким:
$DBConnect = new mysqli("localhost","root","","Ladle");
У меня такая же проблема. Я изменил параметр localhost в объекте mysqli на "127.0.0.1" вместо записи "localhost". Это сработало; Я не уверен, как или почему.
$db_connection = new mysqli("127.0.0.1","root","","db_name");
Надеюсь, это поможет.
Проверьте, нет ли имени db "_" или "-" что помогает в моем случае