PDO语句问题,使用bindParam时查询不起作用
我无法弄清楚这一点。这是我的变量:
$id = 1;
$exp = 1;
$energy = 1;
$payout = rand ( 100 , 300 );
这是查询
$sql = 'update players set ' .
'exp = exp + :exp, ' .
'energy = energy - :energy, ' .
'cash = cash + :cash ' .
'where id = :id';
作为字符串的示例如下:
update players set exp = exp + 1, energy = energy - 1, cash = cash + 170 where id = '1'
当我在 phpmyadmin 中输入它时,它会起作用。这就是为什么我不明白为什么这不起作用:
try
{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':exp', $exp, PDO::PARAM_INT);
$stmt->bindParam(':energy', $energy, PDO::PARAM_INT);
$stmt->bindParam(':cash', $payout, PDO::PARAM_INT);
$result = $stmt->execute();
$stmt->closeCursor();
}
catch (Exception $e)
{
die ($e->getMessage() );
}
if ( !$result )
{
trigger_error("mysql: " . $sql, E_USER_ERROR);
$data = array( "message" => T_gettext("Something went wrong."));
return json_encode( $data );
}
这会导致错误的 $result
= 查询出现问题。
我做错了什么? + 如何从 PDO 获取更详细的 mysql 错误消息?
I can't figure this out. This is my variables:
$id = 1;
$exp = 1;
$energy = 1;
$payout = rand ( 100 , 300 );
here is the query
$sql = 'update players set ' .
'exp = exp + :exp, ' .
'energy = energy - :energy, ' .
'cash = cash + :cash ' .
'where id = :id';
An example of this as a string would be:
update players set exp = exp + 1, energy = energy - 1, cash = cash + 170 where id = '1'
When I input this in phpmyadmin
it works. Thats why I don't understand why this does not work:
try
{
$stmt = $this->db->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':exp', $exp, PDO::PARAM_INT);
$stmt->bindParam(':energy', $energy, PDO::PARAM_INT);
$stmt->bindParam(':cash', $payout, PDO::PARAM_INT);
$result = $stmt->execute();
$stmt->closeCursor();
}
catch (Exception $e)
{
die ($e->getMessage() );
}
if ( !$result )
{
trigger_error("mysql: " . $sql, E_USER_ERROR);
$data = array( "message" => T_gettext("Something went wrong."));
return json_encode( $data );
}
This results in a false $result
= something wrong with query.
What have I done wrong? + how do I get a more detailed mysql error message from PDO?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您实际上是否从连接中抛出异常供您捕获?尝试一下,看看是否收到任何错误消息:
Are you actually throwing exceptions from your connection for you to catch? Try this and see if you get any error messages: