致命错误:调用成员函数query()on null [英] Fatal error: Call to a member function query() on null
本文介绍了致命错误:调用成员函数query()on null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道这里发生了什么。我只是在网上教程,这些错误弹出。
I'm not sure what's going wrong here. I was just following a tutorial online and these errors popped up.
我收到以下错误
错误
Notice: Undefined variable: db in C:\xampp\htdocs\wisconsindairyfarmers\admin\login.php on line 7
Fatal error: Call to a member function query() on null in C:\xampp\htdocs\wisconsindairyfarmers\admin\login.php on line 7
代码
<?php
$db = new mysqli('127.0.0.1', 'root', '', 'wisconsindairyfarmers');
?>
<?php
require '../db/connect.php';
require '../functions/general.php';
function user_exists($username){
//$username = sanitize($username);
$result = $db->query("SELECT COUNT(UserId) FROM users WHERE UserName = '$username'");
if($result->num_rows){
return (mysqli_result($query, 0) == 1) ? true : false;
}}
if(empty($_POST) === false){
$username = $_POST['username'];
$password = $_POST['password'];
if(empty($username) === true || empty($password) === true){
echo 'You need to enter a username and password';
}
else if(user_exists($username) === false) {
echo 'We can\'t find that username.';
}
}
?>
推荐答案
如果你想在函数里面使用它,你应该把它放在你的函数代码的开头:
First, you declared $db outside the function. If you want to use it inside the function, you should put this at the begining of your function code:
global $db;
我想,当你写:
if($result->num_rows){
return (mysqli_result($query, 0) == 1) ? true : false;
你真正想要的是:
if ($result->num_rows==1) { return true; } else { return false; }
这篇关于致命错误:调用成员函数query()on null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文