从 PHP MySQL 结果对象转换为结果资源(OO 到过程)
我一直在使用面向对象的风格在 PHP 应用程序中处理 MySQL 数据库函数。但现在我想使用一个需要结果资源作为输入变量的函数,但我所拥有的只是语句中的结果对象
$oresult = $odb->query($sql);
是否有一种方法可以从面向对象样式的数据库连接生成结果资源 ($dbconn = 新 mysqli (...))?
感谢您
I have been handling MySQL database functions within a PHP application using an object oriented style. But now I want to use a function that requires a result resource as an input variable but all I have available is a result object from the statement
$oresult = $odb->query($sql);
Is there a way to produce a result resource from an object oriented style database connection
($dbconn = new mysqli (...))?
Thanks you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您通常希望向结果集提供资源(封装数据库函数的包装函数。即 numRows()、affectedRows() 等),这反过来又可以为您提供记录集以在必要时迭代结果。
You generally want to provide the resource to your result set (wrapper functions to encapsulate the database functions. I.e., numRows(), affectedRows(), etc.), which in turn can provide you with a record set to iterate the results if necessary.
AFAIK,Mysqli 的 OO 和 Procedural 风格甚至根本不处理结果资源。
query()
和real_query()
函数和方法都会立即(或最终导致)返回一个Mysqli_result
对象 如果返回记录集。整个 Mysqli 扩展中没有函数或属性可以从主Mysqli
对象中提取“链接”,也没有从Mysqli_result
对象中提取资源(本身)。事实上,您在整个 Mysqli 过程风格示例中看到的$link
实际上只是一个Mysqli
对象。如果您使用的任何函数调用“结果资源”,则它们必须来自旧的常规 Mysql 扩展的函数。没有办法将 Mysql 和 Mysqli 函数和方法混合在一起。所以本质上,是的,在这种情况下你必须完全放弃 Mysqli,并从一开始就使用旧式的、过程化的 Mysql。
AFAIK, both OO and Procedural styles of Mysqli do not even deal out result resources at all. The
query()
andreal_query()
functions and methods all immediately (or eventually lead to) return aMysqli_result
object if a recordset is returned. There is no function or property throughout the Mysqli extension to extract the "link" from the mainMysqli
object, nor to extract the resource (per se) from theMysqli_result
object. In fact, that$link
you see referenced throughout the procedural style examples of Mysqli is actually just aMysqli
object.If any function you're using calls for a "result resource", they would have to mean coming from a function of the old, regular Mysql extension. There is no way to blend Mysql and Mysqli functions and methods together. So essentially, yes, you would have to forgo Mysqli entirely in this case, and start with old-style, procedural Mysql from the very beginning.