Jump to content

[SOLVED] Why is this not working? Call to a member function query() on a non-object


heldenbrau

Recommended Posts

$mysqli = new mysqli("localhost", "username", "password", "user");
    if ($mysqli === false) {
      die("ERROR: Could not connect to database. " . mysqli_connect_error());
    }

    $defun=$mysqli->escape_string($defendant);
    $sql="SELECT email FROM users WHERE username='$defun'";
    if ($result = $msyqli->query($sql)) {
      $row = $result->fetch_assoc();
      $mail = $row['email'];
    }else{
      die("No user could be found with that username");
    }

 

I always get info from the database with this method.  But in this one bit of the program on one php file, I get the error:

 

Fatal error: Call to a member function query() on a non-object

 

The line it has a problem with is if ($result = $msyqli->query($sql)) {

 

What does the error actually mean, and what would cause it to happen?

It means exactly what it says.  You're calling a member function, query, on an object, $mysqli.  Except the object is not an object (the error message says non-object) so it has no member functions and the code crashes.

 

I suggest adding a var_dump( $mysqli ) right before the bad line; then you'll see what type of variable $mysqli really is.

It says NULL.

 

I have been working on this problem all night and have found a solution, but I don't understand fuly what is going on.

 

If I close $mysql after each query and then start a new $mysql and log in again, then it works, even though it is the same database I am using.  Do I have to do this every time I want to start a new query?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.