php 如何两次通过mysql结果?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6439230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:24:01  来源:igfitidea点击:

How to go through mysql result twice?

phpmysql

提问by A-OK

For whatever reason I need to go through a mysql result set twice. Is there a way to do it? I don't want to run the query twice and I don't want to have to rewrite the script so that it stores the rows somewhere and then reuses them later.

无论出于何种原因,我都需要两次通过 mysql 结果集。有没有办法做到这一点?我不想运行查询两次,也不想重写脚本以便它将行存储在某处,然后在以后重用它们。

回答by Shef

This is how you can do it:

你可以这样做:

$result = mysql_query(/* Your query */);
while($row = mysql_fetch_assoc($result)){
 // do whatever here...
}

// set the pointer back to the beginning
mysql_data_seek($result, 0);
while($row = mysql_fetch_assoc($result)){
 // do whatever here...
}

However, I would have to say, this doesn't seem the right way to handle this. Why not do the processing within the first loop?

但是,我不得不说,这似乎不是处理此问题的正确方法。为什么不在第一个循环内进行处理?

回答by phant0m

Try whether mysql_data_seek()does what you need.

尝试mysql_data_seek()是否满足您的需求。

mysql_data_seek() moves the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number. The next call to a MySQL fetch function, such as mysql_fetch_assoc(), would return that row.

row_number starts at 0. The row_number should be a value in the range from 0 to mysql_num_rows() - 1. However if the result set is empty (mysql_num_rows() == 0), a seek to 0 will fail with a E_WARNING and mysql_data_seek() will return FALSE

mysql_data_seek() 将与指定结果标识符关联的 MySQL 结果的内部行指针移动到指定的行号。对 MySQL fetch 函数的下一次调用,例如 mysql_fetch_assoc(),将返回该行。

row_number 从 0 开始。 row_number 应该是从 0 到 mysql_num_rows() - 1 范围内的值。但是,如果结果集为空 (mysql_num_rows() == 0),则对 0 的查找将失败并显示 E_WARNING 和 mysql_data_seek () 将返回 FALSE

回答by guitarlass

For mysqliyou should do the following;

对于mysqli,您应该执行以下操作;

$result= $con->query($sql); // $con is the connection object
$result->data_seek(0); 

回答by Clintonio

Alternative to the data seek is to store the values into an array:

数据查找的替代方法是将值存储到数组中:

$arrayVals = array();
$result = mysql_query(/* Your query */);
while($row = mysql_fetch_assoc($result)){
    $arrayVals[] = $row;
}

// Now loop over the array twice instead

$len = count($arrayVals);
for($x = 0; $x < $len; $x++) {
    $row = $arrayVals[$x];

    // Do something here    
}

$len = count($arrayVals);
for($x = 0; $x < $len; $x++) {
    $row = $arrayVals[$x];

    // Do something else here   
}

回答by BugFinder

I confess I haven't tried this, but have you tried after your first iteration

我承认我没有尝试过这个,但是你在第一次迭代后尝试过吗

mysql_data_seek($queryresult,0);

to go to the first record?

去第一条记录?

回答by Steve

You can use mysql_data_seekto move the internal pointer to the beginning of the data set. Then, you can just iterate through it again.

您可以使用mysql_data_seek将内部指针移动到数据集的开头。然后,您可以再次遍历它。

回答by Andreas Eriksson

Well, you could always count the number of rows you read, and then do something like this:

好吧,你总是可以计算你阅读的行数,然后做这样的事情:

if (rownumber == mysql_num_rows($result)) { mysql_data_seek($result, 0); }

Don't know why you would need to, but there it is.

不知道你为什么需要,但它就在那里。