0

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!

2
Contributors
5
Replies
20
Views
1 Year
Discussion Span
Last Post by James_43
2

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"] ) ); 

?

Votes + Comments
Well intended & extensive
0

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

0

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.

0

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.

0

Yeah, that solved everything. Silly mistake.

Thanks for your help :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.