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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.