©
This document uses PHP Chinese website manual Release
(PHP 4, PHP 5)
mysql_data_seek — 移动内部结果的指针
$result
, int $row_number
)mysql_data_seek() 将指定的结果标识所关联的 MySQL 结果内部的行指针移动到指定的行号。接着调用 mysql_fetch_row() 将返回那一行。
row_number
从 0 开始。row_number
的取值范围应该从 0 到 mysql_num_rows - 1。但是如果结果集为空( mysql_num_rows()
== 0),要将指针移动到 0 会失败并发出 E_WARNING
级的错误, mysql_data_seek() 将返回 FALSE
。
result
resource 型的结果集。此结果集来自对 mysql_query() 的调用。
row_number
想要设定的新的结果集指针的行数。
成功时返回 TRUE
, 或者在失败时返回 FALSE
。
Example #1 mysql_data_seek() 例子
<?php
$link = mysql_connect ( 'localhost' , 'mysql_user' , 'mysql_password' );
if (! $link ) {
die( 'Could not connect: ' . mysql_error ());
}
$db_selected = mysql_select_db ( 'sample_db' );
if (! $db_selected ) {
die( 'Could not select database: ' . mysql_error ());
}
$query = 'SELECT last_name, first_name FROM friends' ;
$result = mysql_query ( $query );
if (! $result ) {
die( 'Query failed: ' . mysql_error ());
}
for ( $i = mysql_num_rows ( $result ) - 1 ; $i >= 0 ; $i --) {
if (! mysql_data_seek ( $result , $i )) {
echo "Cannot seek to row $i : " . mysql_error () . "\n" ;
continue;
}
if (!( $row = mysql_fetch_assoc ( $result ))) {
continue;
}
echo $row [ 'last_name' ] . ' ' . $row [ 'first_name' ] . "<br />\n" ;
}
mysql_free_result ( $result );
?>
Note:
本扩展自 PHP 5.5.0 起已废弃,并在将来会被移除。应使用 MySQLi 或 PDO_MySQL 扩展来替换之。参见 MySQL:选择 API 指南以及相关 FAQ 以获取更多信息。用以替代本函数的有:
- mysqli_data_seek()
PDO::FETCH_ORI_ABS
Note:
mysql_data_seek() 只能和 mysql_query() 结合起来使用,而不能用于 mysql_unbuffered_query() 。
[#1] saeed at photobookworldwide dot com [2011-11-25 18:30:04]
Here, you can find the current pointer of selected row easily:
<?php
//selected row with id=4
$id = "4";
$result = mysql_query("select * from jos_components");
$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++){
mysql_data_seek($result,$i);
$row = mysql_fetch_assoc($result);
if($row['id'] == $id){
$pointer = $i;
}
}
// current pointer for selected row
echo $pointer;
?>
[#2] Daniel [2008-08-28 14:59:19]
Here is a simple function to "peek" at the position of the internal pointer in a query result:
<?php
function mysql_pointer_position($result_set) {
$num_rows = mysql_num_rows($result_set);
$i = 0;
while($result = mysql_fetch_array($result_set)) {
$i++;
}
$pointer_position = $num_rows - $i;
//Return pointer to original position
if($pointer_position <= $num_rows - 1) {
mysql_data_seek($result_set, $pointer_position);
}
return $pointer_position;
}
?>
[#3] [2006-05-30 01:52:11]
A helpful note about the 'resource' data type.
Since the 'resource' variable is pointing to a row in a result set at any given time, you can think of it as being passed to this function by reference every time you pass it or assign it to a variable.
<?php
$sql = "SELECT * from <table>";
$result = mysql_query($sql);
$temp_result = $result;
while ($row = mysql_fetch_assoc($temp_result)) {
// do stuff with $row
}
while ($row = mysql_fetch_assoc($result)) {
// This code will never run because the 'resource' variable is pointing past the end of the result set,
// even though it was *not* assigned by reference to $result2.
}
?>
Therefore, the following snipits are functionally identical:
<?php
// Start snipit 1
$sql = "SELECT * from <table>";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
// do stuff with $row
}
mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($result)) {
// do other stuff with $row
}
// Start snipit 2
$sql = "SELECT * from <table>";
$result = mysql_query($sql);
$temp_result = $result;
while ($row = mysql_fetch_assoc($temp_result)) {
// do stuff with $row
}
mysql_data_seek($result, 0);
while ($row = mysql_fetch_assoc($temp_result)) {
// do other stuff with $row
}
?>
[#4] arturo_b at hotmail dot com [2005-04-20 20:53:47]
[#5] b.steinbrink at g m x dot de [2004-12-08 23:09:35]
to kennethnash1134 at yahoo dot com
your loop can be done like this as well and i guess this is faster:
$r=mysql_query("select user,id,ip from accounts limit 10");
unset($users); // Just to be sure
while($users[] = mysql_fetch_row);
array_pop($users); // Drop the last entry which is FALSE
[#6] kennethnash1134 at yahoo dot com [2004-03-26 00:12:05]
// simple example query
$r=mysql_query("select user,id,ip from accounts limit 10");
//starts the for loop, using mysql_num_rows() to count total
//amount of rows returned by $r
for($i=0; $i<mysql_num_rows($r); $i++){
//advances the row in the mysql resource $r
mysql_data_seek($r,$i);
//assigns the array keys, $users[row][field]
$users[$i]=mysql_fetch_row($r);
}
//simple, hope someone can use it :)
// -Kenneth Nash