3

在 CentOS 6.2 上使用 PHP 5.3.3、PostgreSQL 8.4.11、pgbouncer 1.3.4(在session模式下)我正在尝试执行几个 SQL 命令并通过 PHP 脚本获取结果。

当我将命令从脚本复制到psql提示符时,它们可以完美运行并返回 12 行:

在此处输入图像描述

但是当我从脚本运行时,我得到了错误:

SQLSTATE[42601]: Syntax error: 7 ERROR: cannot insert multiple commands into a prepared statement

请问有什么帮助吗?

下面是我失败的 PHP 代码,我尝试过使用$db->query()而不是$db->prepare/execute

try {
        $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
        $db = new PDO(sprintf('pgsql:host=%s port=%u; dbname=%s',
                DBHOST, DBPORT, DBNAME), DBUSER, DBPASS, $options);

        $sth = $db->prepare("
            start transaction;
            create temporary table temp_ids (id varchar not null) on commit drop;
            insert into temp_ids (id)
                    select id
                    from pref_money
                    where yw = to_char(current_timestamp - interval '1 week', 'IYYY-IW')
                    order by money
                    desc limit 10;

            create temporary table temp_rids (rid integer not null) on commit drop;
            insert into temp_rids (rid)
                    select rid
                    from pref_cards
                    where stamp > now() - interval '1 day' and
                    id in (select id from temp_ids) and
                    bid = 'Мизер' and
                    trix > 0;

            SELECT r.rid, r.cards, to_char(r.stamp, 'DD.MM.YYYY HH24:MI') as day,
                    c.bid, c.trix, c.pos, c.money, c.last_ip, c.quit,
                    u.id, u.first_name, u.avatar, u.female, u.city, u.vip > CURRENT_DATE as vip
                    FROM pref_rounds r, pref_cards c, pref_users u
                    WHERE u.id = c.id and
                        r.rid = c.rid and
                        r.rid in (select rid from temp_rids)
                    order by rid, pos;
            commit;
        ");
        $sth->execute();
        while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
              # stuff a JSON object
        }
} catch (Exception $e) {
        exit('Database problem: ' . $e->getMessage());
}
4

2 回答 2

3

试试这个,开始一个事务并拆分你的查询,因为你没有从用户输入中插入值,所以不需要准备查询,也因为你不期望任何结果,但最后一个 exec 很好。在最后一个上,您可以使用 query()。如果发生异常,您可以回滚更改。

<?php 
try {
    $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
    $db = new PDO(sprintf('pgsql:host=%s port=%u; dbname=%s',
    DBHOST, DBPORT, DBNAME), DBUSER, DBPASS, $options);
    //Transaction
    $db->beginTransaction();

    $db->exec("create temporary table temp_ids (id varchar not null) on commit drop;");

    $db->exec("insert into temp_ids (id)
                    select id
                    from pref_money
                    where yw = to_char(current_timestamp - interval '1 week', 'IYYY-IW')
                    order by money
                    desc limit 10;");

    $db->exec("create temporary table temp_rids (rid integer not null) on commit drop;");

    $db->exec("insert into temp_rids (rid)
                    select rid
                    from pref_cards
                    where stamp > now() - interval '1 day' and
                    id in (select id from temp_ids) and
                    bid = 'Мизер' and
                    trix > 0;");
    //Commit changes before doing your select
    $db->commit();

    $sth = $db->query("SELECT r.rid, r.cards, to_char(r.stamp, 'DD.MM.YYYY HH24:MI') as day,
                    c.bid, c.trix, c.pos, c.money, c.last_ip, c.quit,
                    u.id, u.first_name, u.avatar, u.female, u.city, u.vip > CURRENT_DATE as vip
                    FROM pref_rounds r, pref_cards c, pref_users u
                    WHERE u.id = c.id and
                        r.rid = c.rid and
                        r.rid in (select rid from temp_rids)
                    order by rid, pos;");


    while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
        # stuff a JSON object
    }
} catch (Exception $e) {
    //Transaction rollback
    $db->rollback();
    exit('Database problem: ' . $e->getMessage());
}

?>
于 2012-06-13T08:42:00.540 回答
2

我在邮件列表上有一个提示可以摆脱临时工。表,它对我有用:

select r.rid, r.cards, to_char(r.stamp, 'DD.MM.YYYY HH24:MI') as day,
    c.bid, c.trix, c.pos, c.money, c.last_ip, c.quit,
    u.id, u.first_name, u.avatar, u.female, u.city, u.vip > CURRENT_DATE as vip
    from pref_rounds r, pref_cards c, pref_users u
    where u.id = c.id and
    r.rid = c.rid and
    r.rid in (
        select rid
            from pref_cards
            where stamp > CURRENT_TIMESTAMP - interval '1 day' and
            id in (
                select id
                    from pref_money
                    where yw = to_char(CURRENT_TIMESTAMP - interval '1 week', 'IYYY-IW')
                    order by money
                    desc limit 10) and
                   bid = 'Misere' and
                   trix > 0
    )
    order by r.rid, c.pos
于 2012-06-16T13:33:45.383 回答