mysqli why does this happens?

爷,独闯天下 提交于 2020-01-30 10:25:08

问题


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

Fatal error: Call to a member function bind_param() on a non-object in ...

Why this happens? Does this means that I need to open two different connection? Is there any way to avoid this (I love keeping the SQL connection details in one file)?

Here 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();

回答1:


Check the return value of mysqli::prepare. If it is FALSE, you should get the details for the occured error with mysqli::error.




回答2:


I think your $stmt variable is null when you call bind_param over it. maybe your $_POST['vote'] is empty? you can check it before you bind the param on the command




回答3:


Something might have gone wrong with $db->prepare(), check $db->error.



来源:https://stackoverflow.com/questions/725679/mysqli-why-does-this-happens

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!