Why you should not use mysql_fetch_assoc more than 1 time?(为什么你不应该使用 mysql_fetch_assoc 超过 1 次?)
问题描述
有人说你不应该多次使用mysql_fetch_assoc,这是为什么呢?
Some people say you should not use mysql_fetch_assoc more than one time, why is that?
例如:我想显示两张表,一张是支付会员费的用户,另一张是没有支付会员费的用户,所以我不是查询数据库两次,而是查询一次并获得 $result 变量使用这两种类型的用户,然后我运行循环 mysql_fetch_assoc 并查看 list['membership'] = 'paid' 然后 echo ...
e.g.: I want to display two tables one with users who paid for membership, other with users who did not, so instead of querying database 2 times I query it one time and get $result variable with both types of users then I run loop mysql_fetch_assoc and see if list['membership'] = 'paid' then echo ...
第二次我循环 mysql_fetch_assoc 并查看 list['membership'] = 'free' 然后 echo ...
Second time I loop loop mysql_fetch_assoc and see if list['membership'] = 'free' then echo ...
考虑到注册和未注册的用户数量大致相等,什么使用更少的资源.
What uses less resources considering I got about equal amount of users who registered and unregistered.
推荐答案
把你的查询结果集想象成一根香肠,把 mysql_fetch_assoc() 想象成一把切下那根香肠的刀.每次取一行时,都会切下另一根香肠,而且总是新的一根香肠.你不能去切掉之前切过的一块,因为它已经被吃掉了.
Think of your query result set as a sausage, and mysql_fetch_assoc() as a knife that slices off a piece of that sausage. Every time you fetch a row, another piece of sausage is cut off, and it's always a NEW piece of sausage. You can't go and cut off a previously cut piece, because it's been eaten already.
这篇关于为什么你不应该使用 mysql_fetch_assoc 超过 1 次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么你不应该使用 mysql_fetch_assoc 超过 1 次?
基础教程推荐
- php中的PDF导出 2022-01-01
- 将变量从树枝传递给 js 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- php中的foreach复选框POST 2021-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
