-2

Ok so I am learning MySQLi and I am having a hard time getting my login script to behave. It is giving me a call to member function query() on a non-object error. From what I have read, this is likely because I do not have MySQLi in scope. I can't see where the error is and would love it if someone could point it out and explain the issue to me.

<?php session_start(); // Start PHP
// Get info sent to server from login form.
$my_username = $_POST['username'];
$my_password = $_POST['password'];
// MD5 Encrypt the password.
$my_password_md5 = md5($my_password);
// Define MySQL Information.
$db = new MySQLi('localhost', 'DBUSERNAME', 'DBPASS!', 'DB');
if ($db->connect_error) {
$error = $db->connect_error;
}
// Check table for username provided.
$sql="SELECT * FROM TABLE WHERE username='$my_username' and password='$my_password_md5'".
//this is where the error occurs
$result=$db->conn->query($sql) or die("Error in the consult.." . mysqli_error($db));
$rows=mysqli_fetch_assoc($result);
// Count how many rows match that information.
$count=mysqli_num_rows($result);
// Check if there are any matches.
if($count==1){
// If so, register $my_username, $my_password and redirect to the index page.
ini_set("session.gc_maxlifetime", "18000"); 
session_cache_expire(18000);
$cache_expire = session_cache_expire();
$_SESSION['username'] = $my_username;
$_SESSION['id'] = $rows['id'];
header("location:WEBSITE");
}

// If not, redirect back to the index page and provide an error.
else {
header("location:WEBSITE");
}
// End PHP
?>
Tyler Radlick
  • 184
  • 1
  • 6
  • 12

1 Answers1

1

Call to a member function query() on a non-object means that you are trying to call a function query() on something that is not an object. You are calling $db->conn->query($sql), so $db->conn is not an object. $db is a Mysqli object. Reading the documentation reveals that there is no such thing as MySQLi::conn.

I assume you want to call MySQLi::query(...) (docs). Change the code to $db->query($sql) and your problem vanishes.

Onimusha
  • 3,348
  • 2
  • 26
  • 32
Sumurai8
  • 20,333
  • 11
  • 66
  • 100