php mysqli::query(): 无法获取 mysqli
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19937880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysqli::query(): Couldn't fetch mysqli
提问by codingManiac
Warning: mysqli::query(): Couldn't fetch mysqli in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\my portable files\class_EventCalendar.php on line 43
警告:mysqli::query(): 无法在第 43 行的 C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\my Portable files\class_EventCalendar.php 中获取 mysqli
The following is my connection file:
以下是我的连接文件:
<?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.".";
}
?>
This is my class:
这是我的课:
<?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);
I'm not great with OOP PHP and I'm not sure why this error is being raised. I pulled this code from elsewhere and the only thing I changed was the @new mysqli
parameters. Can anyone help me to understand what is going wrong?
我不擅长 OOP PHP,我不确定为什么会出现这个错误。我从别处提取了这段代码,唯一改变的是@new mysqli
参数。任何人都可以帮助我了解出了什么问题吗?
回答by T.Todua
Probably somewhere you have DBconnection->close();
and then some queries try to execute .
可能在某个地方DBconnection->close();
,然后一些查询尝试执行。
Hint: It's sometimes mistake to insert ...->close();
in __destruct()
(because __destruct
is event, after which there will be a need for execution of queries)
提示:它有时错误插入...->close();
的__destruct()
(因为__destruct
是大事,在这之后会有需要查询的执行)
回答by Aycan Ya??t
Reason of the error is wrong initialization of the mysqli object. True construction would be like this:
错误原因是 mysqli 对象初始化错误。真正的构造应该是这样的:
$DBConnect = new mysqli("localhost","root","","Ladle");
回答by Ratnadeep Chakraborty
I had the same problem. I changed the localhost parameter in the mysqli object to '127.0.0.1' instead of writing 'localhost'. It worked; I'm not sure how or why.
我有同样的问题。我将 mysqli 对象中的 localhost 参数更改为“127.0.0.1”,而不是写入“localhost”。有效; 我不确定如何或为什么。
$db_connection = new mysqli("127.0.0.1","root","","db_name");
Hope it helps.
希望能帮助到你。
回答by Gorodeckij Dimitrij
Check if db name do not have "_" or "-" that helps in my case
检查数据库名称是否没有对我有帮助的“_”或“-”