简体   繁体   English

致命错误:在非对象错误上调用成员函数 query()

[英]Fatal error: Call to a member function query() on a non-object Error

I have a small problem.我有一个小问题。 I'm trying to make a simple register/login system with sessions and I got this error:我正在尝试使用会话制作一个简单的注册/登录系统,但出现此错误:

Fatal error: Call to a member function query() on a non-object in C:\\xampp\\htdocs\\members\\includes\\login.inc.php on line 9致命错误:第 9 行在 C:\\xampp\\htdocs\\members\\includes\\login.inc.php 中的非对象上调用成员函数 query()

This is the relevant line of code:这是相关的代码行:

$result = $conn->query($sql);

The first time I tried it was working.我第一次尝试它是有效的。

The rest of the code:其余代码:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $email      = $_POST['email'];
    $password   = md5($_POST['password']);
    $sql = "SELECT email, password FROM member WHERE email = '$email' AND password = '$password'";
    $result = $conn->query($sql);

I also have db.php, which is used to connect the MYSQL and everything inside it is fine.我还有db.php,用来连接MYSQL,里面的一切都很好。

I cannot understand why, the first time I tried it was working I guess and now this kind of error.我不明白为什么,我第一次尝试它的时候我猜它可以工作,现在出现这种错误。

I'm also having the db.php which is used to connect the MYSQL and everything inside is fine.. could someone explain me why I keep facing this error ?我还有用于连接 MYSQL 的 db.php,里面的一切都很好..有人能解释一下为什么我一直面临这个错误吗?

I'm going to speculate.我来推测一下。 I'm speculating that you have a separate file (probably called db.php ) which "handles" the setting up of the database connection.我推测您有一个单独的文件(可能称为db.php ),它“处理”数据库连接的设置。 I'm further going to speculate that you've a chain of files which are require() (or include() )'d into your web app.我将进一步推测您有一系列文件,这些文件被require() (或include() )放入您的网络应用程序中。

I've seen this more times than I care to recall.我见过的次数比我想记得的要多。 It's a very old fashioned way of separating code into logical chunks inside PHP - which needs to be left in the past.这是在 PHP 中将代码分成逻辑块的一种非常老式的方法 - 这需要留在过去。

I'm speculating that you were previously defining $conn in another script which was included (or required) before this code.我推测您之前在此代码之前包含(或需要)的另一个脚本中定义了$conn A global variable, which had was dependency later in the code execution.一个全局变量,它在代码执行的后期具有依赖性。 Invisible to the file it was declared in.对声明它的文件不可见。

That's the problem .这就是问题所在 The quick/hack fix is to rename $conn or the restore the original declaration of it and make sure it's global and make sure it is included before this code is ran.快速/黑客修复是重命名$conn或恢复它的原始声明,并确保它是全局的,并确保运行此代码之前包含它。

The proper fix (IMHO) is to look at using a framework (Laravel, Lumen, CodeIgniter, Yii, there are many - take your pick) and read up on the topics of dependency injection, autoloading and namespacing.正确的解决方法(恕我直言)是查看使用框架(Laravel、Lumen、CodeIgniter、Yii,有很多 - 随你选择)并阅读依赖注入、自动加载和命名空间的主题。 Think about why global variable declarations make for unmaintainable code.想一想为什么全局变量声明会导致无法维护的代码。

If you're really reluctant to go with a full framework, at the very least have a look at some database-abstraction libraries like doctrine (and it's sub-library dbal ) which can easily be auto-loaded into your project via composer .如果你真的不愿意使用一个完整的框架,至少可以看看一些数据库抽象库,比如doctrine (它是子库dbal ),它们可以很容易地通过composer自动加载到你的项目中。

As Sascha already pointed out, $conn might be either not defined at all, or it's not an object (hence the error message).正如Sascha已经指出的那样, $conn可能根本没有定义,或者它不是一个对象(因此是错误消息)。

From the code sample you have provided, it's actually a bit hard to tell what kind of connection object you might be using, but I think it's save to say that in your case it might be either PDO or mysqli .从您提供的代码示例中,实际上很难判断您可能使用的是哪种连接对象,但我认为可以说在您的情况下它可能是PDOmysqli

For the sake of simplicity, let's stick with mysqli.为了简单起见,让我们坚持使用 mysqli。 A working code sample based on mysqli would look like this (shortened example taken from the docs cited above):基于 mysqli 的工作代码示例如下所示(取自上述文档的缩短示例):

$conn   = new mysqli("localhost", "my_user", "my_password", "world");
$result = $conn->query($sql);

Though you really should go for so-called prepared statements, as your code right now is prone to SQL injection as wally already stated.尽管您确实应该使用所谓的准备好的语句,因为正如 wally 已经说过的那样,您现在的代码很容易受到 SQL 注入。

I would have linked wally's answer and provide you with a link to the PHP docs relating to prepared statements, but apparently, my lack of reputation points don't allow me to, so just do a quick Google search for PHP & prepared statements.我会链接沃利的答案,并为您提供与准备好的声明相关的 PHP 文档的链接,但显然,我缺乏声誉点不允许我这样做,所以只需在 Google 上快速搜索 PHP 和准备好的声明即可。

The database connection file has to be added at the beginning of the file.必须在文件开头添加数据库连接文件。

The present format is easy.现在的格式很简单。

<?php  $mysqli = new mysqli("localhost", "Userid", "password", "database name"); if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; }
?>

At the beginning:开头:

require 'db.php';需要'db.php'; $conn = new db(); $conn = 新数据库();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:在(CodeIgniter)中的非对象上调用成员函数query() - Fatal error: Call to a member function query() on a non-object in (CodeIgniter) 致命错误:在非对象表达式引擎上调用成员函数query() - Fatal error: Call to a member function query() on a non-object expressionengine 致命错误:在非对象上调用成员函数query() - Fatal error: Call to a member function query() on a non-object 致命错误:在非对象上调用成员函数query() - Fatal error: Call to a member function query() on a non-object 致命错误:在非对象中调用成员函数query() - Fatal error: Call to a member function query() on a non-object in Codeigniter 致命错误:调用非对象上的成员函数 query() - Codeigniter Fatal error: Call to a member function query() on a non-object 错误致命错误:在非对象上调用成员函数insert() - Error Fatal error: Call to a member function insert() on a non-object 致命错误:在非对象错误上调用成员函数prepare() - Fatal error: Call to a member function prepare() on a non-object ERROR 致命错误:当它是一个对象时,调用非对象上的成员函数 - Fatal Error: Call to member function on non-object when it is an object Magento:致命错误:在非对象上调用成员函数 getMethodInstance() - Magento : Fatal error: Call to a member function getMethodInstance() on a non-object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM