Studio DnB Studio DnB - 6 months ago 12
MySQL Question

Call to a member function on a non object error in mysql prepared statement

I'm adding a Follow profile function to my page.

I have a Follow button which changes to Unfollow if there is a row in database containing profile ID and session ID. If the person clicks on Unfollow I get an error saying:


Fatal error: Call to a member function execute() on a non-object in blah blah line 20


This is the code I'm running:

function isFollow($mysqli){
if(isset($_POST['isFollow']))
{
$fed_id = filter_input(INPUT_POST, 'fed_id', FILTER_SANITIZE_NUMBER_INT);
$fing_id = filter_input(INPUT_POST, 'fing_id', FILTER_SANITIZE_NUMBER_INT);
$isFollow = filter_input(INPUT_POST, 'isFollow', FILTER_SANITIZE_NUMBER_INT);
if($isFollow==0)
{
$fing_time=date("Y-m-d H:i:s");
$insert_stmt = $mysqli->prepare("INSERT INTO follower (fed_user, fing_user, date_time) VALUES (?, ?, ?)");
$insert_stmt->bind_param('iii', $fed_id, $fing_id, $fing_time);
if($insert_stmt->execute())
{
header("Location:../?profile=".$fed_id."");
}
}elseif($isFollow==1)
{
$delete_stmt = $mysqli->prepare("DELETE FROM follower WHERE fed_id = ? AND fing_id = ?");
$delete_stmt->bind_param('ii', $fed_id, $fing_id);
if($delete_stmt->execute())
{
header("Location:../?profile=".$fed_id."");
}
}
}else
{
die("Error!");
}
}


The first part of the code works. It adds in mysql. It just won't delete from it.

Line 20 is

$delete_stmt = $mysqli->prepare("DELETE FROM follower WHERE fed_id = ? AND fing_id = ?");


Please note it works all untill this part:

elseif($isFollow==1)
{
$delete_stmt = $mysqli->prepare("DELETE FROM follower WHERE fed_id = ? AND fing_id = ?");
$delete_stmt->bind_param('ii', $fed_id, $fing_id);
if($delete_stmt->execute())
{
header("Location:../?profile=".$fed_id."");
}
}

Answer

This is most likely due to a failure in prepare. Try printing the error with

trigger_error( $mysqli->error, E_USER_ERROR );

after line 20 and check what went wrong.

You may also want to check of what type and value $delete_stmt is using

print_r( $delete_stmt );

or similar.

Comments