为什么你不应该使用 mysql_fetch_assoc 超过 1 次?

2022-01-05 00:00:00 sql request php mysql

有人说你不应该多次使用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.

相关文章