PDO 连接器无法在具有多个实例的页面上使用
在我的 php 页面的顶部,我包含一个名为
include("connector.php")
Connector.php
<?php
try
{
$db = new PDO('mysql:host=localhost;dbname=DB', 'USER', 'PW');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
echo $e->getMessage();
}
?>
的连接脚本,然后我根据需要在我的页面上进行所有后续调用。然而,特别是在一页上,我最初调用 MySQL 存储过程:
$stmt = $db->prepare("CALL Procedure(:id)");
$stmt->bindParam(':id', $id);
$stmt->execute();
while($row=$stmt->fetch()) {
echo "<tr>
<td>".$row['ID']."</td>
<td>".$row['NAME']."</td>
</tr>";
}
我的存储过程没有问题,但是当我尝试进行任何后续调用时,我会收到通常的 PDO Mysql 无缓冲查询错误:
致命错误:未捕获的异常 带有消息的“PDOException” 'SQLSTATE[HY000]:一般错误:2014 其他时无法执行查询 无缓冲查询处于活动状态。 考虑使用 PDOStatement::fetchAll()。 或者,如果您的代码只是 曾经要对抗mysql,你 可以通过设置启用查询缓冲 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY 属性。'在 C:\page.php:261 堆栈跟踪:#0 C:\page.php(261): PDOStatement->execute(Array) #1 {main} 扔进去 C:\page.php 在第 261 行
是的,我尝试过使用 FetchAll,但不,我不想使用 PDO::MYSQL_ATTR_USE_BUFFERED_QUERY 属性,因为我们可能会在稍后的时间点关闭 MySQL,并且我不希望基于每个数据库进行重建(不是值得一提的是,即使我确实包含它也不起作用)。
任何想法为什么会发生这种情况?我可以在每个数据库调用之前“包含”connector.php 文件,但这似乎是重复的,我必须将每个语句 $db->NULL 。
注意:这似乎只影响调用存储过程的页面。有什么想法吗?
注意:这是在运行 Apache 2.2.14、PHP 5.3.1 的 XAMPP 1.7.3 的 Windows 计算机上运行的
At the top of my php page, I include a connection script called by:
include("connector.php")
Connector.php
<?php
try
{
$db = new PDO('mysql:host=localhost;dbname=DB', 'USER', 'PW');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
echo $e->getMessage();
}
?>
I then make all subsequent calls on my pages as necessary. On one page in particular however, I initially make a call into a MySQL stored procedure:
$stmt = $db->prepare("CALL Procedure(:id)");
$stmt->bindParam(':id', $id);
$stmt->execute();
while($row=$stmt->fetch()) {
echo "<tr>
<td>".$row['ID']."</td>
<td>".$row['NAME']."</td>
</tr>";
}
There is no issue with my stored procedure but down the page when I try and make any subsequent calls, I get the usual PDO Mysql unbuffered queries error:
Fatal error: Uncaught exception
'PDOException' with message
'SQLSTATE[HY000]: General error: 2014
Cannot execute queries while other
unbuffered queries are active.
Consider using
PDOStatement::fetchAll().
Alternatively, if your code is only
ever going to run against mysql, you
may enable query buffering by setting
the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY
attribute.' in
C:\page.php:261
Stack trace: #0
C:\page.php(261):
PDOStatement->execute(Array) #1 {main}
thrown in
C:\page.php
on line 261
Yes, I have tried using FetchAll, and NO I do not want to use the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute as we may switch off MySQL at a later point in time and I do not wish to rebuild based on every DB (not to mention even when I did include it did not work).
Any ideas why this might be happening? I could 'include' the connector.php file before every DB call but that seems repetitive and I'd have to $db->NULL every statement.
Note: this seems to only affect pages with calls into stored procedures. Any ideas?
Note: This is being run on a Windows machine with XAMPP 1.7.3 running Apache 2.2.14, PHP 5.3.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请尝试以下操作: http://bugs.php.net/bug.php?id =39858
查看最后一条评论:1221253320好像已经修复了,你可以尝试将你的 PHP 更新到最新版本。 5.3
我的建议:如果您使用预打包的“服务器”套件(例如 xampp),那么您应该将其全部废弃,使用存储管理中的存储扩展器对硬盘驱动器进行 2GB 分区,分别安装 MySql、PHP、Apache,然后配置它们。
使用 PHP 的最新快照和最新的 MySql Distro,将您的应用程序和数据库迁移到新安装,启动您的计算机,它应该运行良好。
我敢打赌,它也可能归结为 MySql 客户端库,升级它们
我遇到的一些相关链接:
Please try the following: http://bugs.php.net/bug.php?id=39858
Looking at the very last comment: 1221253320 it seems to have been fixed, you can try update your PHP To the latest version. 5.3
my advice: If your using a pre packaged "server" kit such as xampp then you should scrap that all together, partition 2GB of your hard drive using the Storage Extender in Storage Management, Install MySql,PHP,Apache seperatly and then configure them.
use that latest snapshot of PHP and the latest MySql Distro, Migrate your application and database to the new install, kick your computer and it should run fine.
I would bet that it could be down to the MySql Client Libs as well, Upgrade them
Some related links im coming across:
PDO 无缓冲查询和存储过程
PDO 无缓冲查询
PDO Unbuffered queries & Stored Proc.
PDO Unbuffered queries