如题: // 开始事务
$mysqli->autocommit(false); // 设置为不自动提交,因为MYSQL默认立即执行
$stmt = $mysqli->prepare("update seat_table set count = count + 1 where seat_id = ? and count < 50");
$stmt->bind_param("i", $old_seat_id);
if ($stmt->execute()) {
$stmt->close();
$stmt = $mysqli->prepare("update seat_table set count = count-1 where bus_id = ? and seattime = ? and count > 0");
$stmt->bind_param("is", $bus_id, $seattime); if ($stmt->execute()) {
$mysqli->commit(); // 提交
/* 判定事务终止,重新启用负载均衡 */
$mysqli->autocommit(TRUE);
$mysqli->close();
// if ($flag == 1) { // 执行退款业务// }
echo json_encode(array( "code" => "200", "order_id" => $stmt->insert_id
));
} else { // 减库失败
$mysqli->rollback(); die('1500');
}
} else { // 回库失败
$mysqli->rollback(); die('2500');
}首先我这个语句报了错,报Fatal error: Call to a member function bind_param() on a non-object ,报错地方在第一个语句(加库存操作count=count+1),但是无解为什么报错,语句是不会错的,在命令行可正确执行然而就算报错了,却仍然被执行了(十万个程序员问号),结果就是库存加了1,要减的没有变化。我蒙蔽了,大半夜的很头痛。求助问题所在!!非常感谢!!!
2 回答

慕的地6264312
TA贡献1817条经验 获得超6个赞
报错是第一个prepare语句失败了 打印下报错信息看看
要先开启事务 才能将接下来的操作作为一个整体提交或回滚 $o = new Mysqli(...); $o->autocommit(false); $o->begin_transaction();//开启事务...if(成功) { $o->commit(); }else{ $o->rollback(); } $o->autocommit(true);
添加回答
举报
0/150
提交
取消