PDO语句问题,使用bindParam时查询不起作用

发布于 2024-11-07 05:25:17 字数 1302 浏览 0 评论 0原文

我无法弄清楚这一点。这是我的变量:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2024-11-14 05:25:17

您实际上是否从连接中抛出异常供您捕获?尝试一下,看看是否收到任何错误消息:

$db = new PDO($connection_string);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Are you actually throwing exceptions from your connection for you to catch? Try this and see if you get any error messages:

$db = new PDO($connection_string);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文