Hi guys,

I can't get passed this error: Call to a member function bind_param() on a non-object.

I have spent hours looking for insights, and can see that my $statement returns FALSE, but have no idea why?

My understanding of PHP is limited, so it might help if I actually understood what an object was???

I have tried the query straight into mysql and it works fine, so really can't understand why this doesn't work! My code is:

    $datetime = new DateTime();
    $datetime = $datetime->format('Y-m-d H:i:s');

    $query = 'UPDATE vd_users set lastlogin WHERE facebook_id = '.$_SESSION['id'].' VALUES (?)';
    $statement = $mySQL_con->prepare($query);
    $statement->bind_param('s', $datetime);    

    try {
        $statement->execute();

        header('Location: #');
        }

    catch (mysqli_sql_exception $error) {
        die ('Failed to update database');
        }
}

Would really appreciate it if someone could help me out!

Yes to understand what an object is , is something you have to do ;) The second is to read and understand the error messages , you wouldn't need hours to realize that $statement is not an object. You get the PDOStatement object @see http://php.net/manual/en/class.pdostatement.php that you correctly assign it to the $statement variable as a result of a PDO::prepare method call @see http://php.net/manual/en/pdo.prepare.php . That is a method call of a PDO object that in your case you name it $mySQL_con , so in order to have the all picture you share the code of how this object was created.

BUT , you also have problem with your SQL statement alone (would be a good idea first to try an SQL before insert it in a PHP code). @see https://dev.mysql.com/doc/refman/5.0/en/update.html that means that (without knowing your db stracture) you probably wanted to write: UPDATE vd_users SET lastlogin = ? WHERE facebook_id = '.$_SESSION['id'].' . But even that is problematic , with a bind_param 's' (what is s ?)

Wouldn't be better :

$query = "UPDATE vd_users set lastlogin = ? WHERE facebook_id = ?";
$statement = $mySQL_con->prepare( $query );
$statement->execute( array( $datetime , $_SESSION["id"] ) ); 

?

commented: Well intended & extensive +8

set in the SQL query should be best to be capital SET , also what this catch (mysqli_sql_exception does in your code ? You are working with PDO

Thanks for your quick reply!

I've update the code as per your recommendations, so it's now:

    $datetime = new DateTime();
    $datetime = $datetime->format('Y-m-d H:i:s');


    $query = 'UPDATE vd_users SET lastlogin = ? WHERE facebook_id = ?';
    $statement = $mySQL_con->prepare($query);

    try {
        $statement->execute(array($datetime, $_SESSION['id']));
        header('Location: #');
        }

    catch (PDOException $error) {
        echo 'Failed to update database' . $error->getMessage();
        }

Is using the array with execute() better than using bind_params()??

Also, here is the code I use to create the connection:

/Attempt Connection
$mySQL_con = new mysqli($mySQL_host, $mySQL_username, $mySQL_password, $mySQL_database);
//Set UTF8
if (!$mySQL_con->set_charset('utf8')) {
    echo ('<b>Error loading character set utf8<b><br>');// . $mySQL_con->error);
    }

//Check Connection
if ($mySQL_con->connect_error) {
    //Display Error Message
    die("<b>Connection to mySQL Failed:</b><br>Please contact your system administrator immediately.");
    }

But I am still getting the non-object error.

Oh wait, I seem to have tried to combine mysqli and PDO. I think I copied some code from a past project, thinking it was in PDO.

That will be what's causing the problems, I'll recreate the connection with PDO and see what happens.

Yeah, that solved everything. Silly mistake.

Thanks for your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.