有没有办法在 PDO 中设置默认游标类型(特别是 PDO_SQLSRV)?
因此,令人烦恼的是,Microsoft 的 PDO_SQLSRV 驱动程序在使用不可滚动游标的 SELECT 查询调用任何 rowCount() 时都会返回 -1。
进一步增加烦恼的是,默认游标类型是 _FORWARD,这意味着我似乎必须向每个准备好的语句添加属性,我可能需要返回行计数(在这个应用程序中,这是很多),如下所示。这是不可接受的,因为我们将使用的一些数据库引擎不支持可滚动游标。
$pdo->prepare("SELECT x FROM y", array(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL));
奇怪的是,这似乎不适用于 PDO::query()...
编辑: 如下所示,我还尝试了 PDO::setAttribute() 并将选项添加到 PDO::__construct(),但都没有抛出异常或错误,或者似乎实际上没有做任何事情!
我向大家提出的问题是:有没有办法将默认光标类型设置为 CURSOR_SCROLL?
So, annoyingly, the PDO_SQLSRV driver from Microsoft returns -1 on any rowCount() call from a SELECT query with non-scrollable cursors.
Further adding to the annoyance, the default cursor type is _FORWARD, meaning that I am seemingly having to add attributes to every prepared statement where I may need to get a row count back (in this app, that's A LOT) as below. This is unacceptable, as some of the database engines we will be using do not support scrollable cursors.
$pdo->prepare("SELECT x FROM y", array(PDO::ATTR_CURSOR, PDO::CURSOR_SCROLL));
Curiously this does not seem to work with PDO::query()...
Edit:
As below, I've also tried PDO::setAttribute() and adding the option to PDO::__construct(), but neither throw exceptions or errors, or seem to actually do anything!
My question to you all is: is there a way to set the default cursor type to CURSOR_SCROLL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
?
您还可以使用自己的 PDO 基类进行扩展,并覆盖
prepare
方法:What about:
?
Also you can extend base PDO class with your own, with
prepare
method overriden: