Mysqli, why is this happening?

I have two subsequent mysqli statements, and the second returns:

Fatal error: call of bind_param () member function for non-object in ...

Why is this happening? Does this mean that I need to open two different connections? Is there a way to avoid this (I like to store the details of the SQL connection in a single file)?

Here is the code:

$db = new mysqli("localhost", "root", "", "database");

$stmt = $db->prepare("UPDATE posts SET vote_".$_POST['vote']." = vote_".$_POST['vote']." + 1 WHERE id=?");
$stmt->bind_param('s', $_POST['id_post']);
$stmt->execute();
$stmt->close();

$stmt = $db->prepare("INSERT INTO votes (kind, users_id, posts_id) VALUES (?, ?, ?)");
$stmt->bind_param('sss',$_POST['vote'],$_POST['id_user'],$_POST['id_post']);
$stmt->execute();
$stmt->close();
0
source share
3 answers

Check the return value of mysqli :: prepare . If it is FALSE, you should get information about the error that occurred using mysqli :: error .

+1
source

, $stmt null, bind_param . , $_POST ['vote'] ? ,

+1

Perhaps something happened to $db->prepare(), note $db->error.

0
source

All Articles