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

发布于 2024-11-02 15:24:55 字数 401 浏览 1 评论 0原文

有人说你不应该多次使用mysql_fetch_assoc,为什么呢?

例如:我想显示两个表,一个包含已付费会员的用户,另一个包含未付费会员的用户,因此我不是查询数据库两次,而是查询一次并获取两种类型的 $result 变量用户,然后我运行循环 mysql_fetch_assoc 并查看 list['membership'] = 'paid' 然后 echo ...

第二次循环 mysql_fetch_assoc code> 并查看是否 list['membership'] = 'free' 然后 echo ...

考虑到注册和未注册的用户数量大约相同,什么使用更少的资源。

Some people say you should not use mysql_fetch_assoc more than one time, why is that?

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 ...

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北方。的韩爷 2024-11-09 15:24:55

将查询结果集视为一根香肠,将 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.

春风十里 2024-11-09 15:24:55

由 Typer85 引用(链接):

请注意,您传递给此函数的资源结果可以被视为通过引用传递,因为资源只是指向内存位置的指针。

因此,在将指针重置回起始位置之前,您无法在同一脚本中循环访问资源结果两次。

例如:

<?php

// Assume We Already Queried Our Database.

// Loop Through Result Set.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

// We looped through the resource result already so the
// the pointer is no longer pointing at any rows.

// If we decide to loop through the same resource result
// again, the function will always return false because it
// will assume there are no more rows.

// So the following code, if executed after the previous code
// segment will not work.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

// Because $queryContent is now equal to FALSE, the loop
// will not be entered.

?>

解决这个问题的唯一方法是重置指针,使其在第二个代码段之前再次指向第一行,所以现在完整的代码如下所示:

<?php

// Assume We Already Queried Our Database.

// Loop Through Result Set.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

// Reset Our Pointer.

mysql_data_seek( $queryResult );

// Loop Again.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

?>

当然,您必须进行额外的检查以确保结果中的行数不为0,否则mysql_data_seek本身将返回false 并且会引发错误。

另请注意,这适用于获取结果集的所有函数,包括 mysql_fetch_rowmysql_fetch_assosmysql_fetch_array

Quote by Typer85 (link):

Please be advised that the resource result that you pass to this function can be thought of as being passed by reference because a resource is simply a pointer to a memory location.

Because of this, you can not loop through a resource result twice in the same script before resetting the pointer back to the start position.

For example:

<?php

// Assume We Already Queried Our Database.

// Loop Through Result Set.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

// We looped through the resource result already so the
// the pointer is no longer pointing at any rows.

// If we decide to loop through the same resource result
// again, the function will always return false because it
// will assume there are no more rows.

// So the following code, if executed after the previous code
// segment will not work.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

// Because $queryContent is now equal to FALSE, the loop
// will not be entered.

?>

The only solution to this is to reset the pointer to make it point at the first row again before the second code segment, so now the complete code will look as follows:

<?php

// Assume We Already Queried Our Database.

// Loop Through Result Set.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

// Reset Our Pointer.

mysql_data_seek( $queryResult );

// Loop Again.

while( $queryContent = mysql_fetch_row( $queryResult ) {

    // Display.

    echo $queryContent[ 0 ];
}

?>

Of course you would have to do extra checks to make sure that the number of rows in the result is not 0 or else mysql_data_seek itself will return false and an error will be raised.

Also please note that this applies to all functions that fetch result sets, including mysql_fetch_row, mysql_fetch_assos, and mysql_fetch_array.

迟月 2024-11-09 15:24:55

当有人说你不能调用 mysql_fetch_assoc() 两次,它们意味着针对相同的资源。您传递给 mysql_fetch_assoc() 的资源结果是通过引用完成的。在再次使用 mysql_fetch_assoc() 之前,您需要重置指针的位置。

编辑:为此,请尝试使用 mysql_data_seek()

When someone says you can't call mysql_fetch_assoc() twice, they mean against the same resource. The resource result you pass in to mysql_fetch_assoc() is done by reference. You'll need to reset the position of the pointer before you can use mysql_fetch_assoc() a second time.

EDIT: And to do so, try using mysql_data_seek().

无风消散 2024-11-09 15:24:55

看来您想要做的是将查询结果视为数组(字段)的数组(行)。但这并不是 mysql 库真正提供的。事实上,我经常做的是将行复制到数组数组中(只需在 mysql_fetch 上循环直到为空),然后使用 PHP 为此目的提供的数组函数对我自己的行集执行我想要的操作。这也最大限度地减少了表被锁定的时间。

It appears that what you want to do is to treat the query result as an array (rows) of arrays(fields). But that's not really what the mysql library provides. What I will often do is in fact copy the rows into an array of arrays (just loop on mysql_fetch until empty) and then do what I want with my own rowset using array functions that PHP provides for the purpose. This also minimizes the amount of time the table is locked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文