php:迭代记录集 - 更简单的方法?
我刚刚从 ASP 更改为 php,我对 php 处理记录集的方式有点困惑。 我想知道是否有更简单的方法通过创建 php 类来迭代记录集。 这是 ASP 语法来说明我的意思:
sq = "select * from myData"
set rs = db.execute(sq)
do while not rs.eof
response.write rs("name") // output data (response.write = echo)
rs.movenext
loop
有什么想法吗? 谢谢
i've just changed from ASP to php and i'm a bit confused about the way php is handling recordsets.
i'd like to know if there's an easier way to iterate a recordset by creating a php class.
here's the ASP syntax to show what i mean:
sq = "select * from myData"
set rs = db.execute(sq)
do while not rs.eof
response.write rs("name") // output data (response.write = echo)
rs.movenext
loop
any ideas?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你几乎会做同样的事情......
You'd pretty much do the same thing...
您可能正在寻找名称中包含单词
fetch
的函数。例如
mysql_fetch_assoc()
或$pdo->fetchAll()
。PHP 中的大多数数据库 API 函数都会返回某种称为“资源”的指针变量,该变量可以传递给 fetch-family 函数,如下所示:
但是,其中一些(例如 PDO 的 fetchAll 方法)返回常规 PHP 数组,您可以将其用作常规
foreach
运算符进行迭代。You're probably looking for a function contains word
fetch
in it's name.E.g.
mysql_fetch_assoc()
or$pdo->fetchAll()
.Most of database API functions in PHP returns some sort of pointer variable called "resource", which can be passed to the fetch-family function, like this:
However, some of them (like PDO's fetchAll method) returns but regular PHP array, which you can iterate using as regular
foreach
operator.