mysqli为什么会发生这种情况? [英] mysqli why does this happens?

查看:58
本文介绍了mysqli为什么会发生这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个后续的mysqli语句,第二个返回:

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

致命错误:在...中的非对象上调用成员函数bind_param()

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

为什么会这样?这是否意味着我需要打开两个不同的连接? 有什么方法可以避免这种情况(我喜欢将SQL连接详细信息保存在一个文件中)?

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)?

代码在这里:

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

推荐答案

检查 mysqli :: prepare .如果为假,则应该使用 mysqli :: error .

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

这篇关于mysqli为什么会发生这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆