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 次?


- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01