在 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());
}