I'm not sure what's going wrong here. I was just following a tutorial online and these errors popped up.
I'm getting the following errors
Error
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
Code
<?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;
And I guess, when you wrote:
if($result->num_rows){
return (mysqli_result($query, 0) == 1) ? true : false;
what you really wanted was:
if ($result->num_rows==1) { return true; } else { return false; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With