mysql_data_seek

(PHP 3, PHP 4, PHP 5)

mysql_data_seek -- 移动内部结果的指针

说明

bool mysql_data_seek ( resource result_identifier, int row_number )

如果成功则返回 TRUE,失败则返回 FALSE

mysql_data_seek() 将指定的结果标识所关联的 MySQL 结果内部的行指针移动到指定的行号。接着调用 mysql_fetch_row() 将返回那一行。

row_number 从 0 开始。row_number 的取值范围应该从 0 到 mysql_num_rows - 1。

注: mysql_data_seek() 只能和 mysql_query() 结合起来使用,而不能用于 mysql_unbuffered_query()

例子 1. MySQL 数据检索例子

<?php
    $link
= mysql_pconnect("localhost", "mysql_user", "mysql_password")
        or die(
"Could not connect: " . mysql_error());

    
mysql_select_db("samp_db")
        or die(
"Could not select database: " . mysql_error());

    
$query = "SELECT last_name, first_name FROM friends";
    
$result = mysql_query($query)
        or die(
"Query failed: " . mysql_error());

    
/* fetch rows in reverse order */
    
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_object($result)))
            continue;

        echo
"$row->last_name $row->first_name<br />\n";
    }

    
mysql_free_result($result);
?>

参见 mysql_query()mysql_num_rows()


add a note add a note User Contributed Notes
30-May-2006 04:52
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.

<?

$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:

<?

// 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
}

?>
jonybd at yahoo dot com
27-Jun-2005 08:40
/*
   helpfull for real time databases query
   - Query one time
   - Retreive data twice from the same query
   - mysql_data_seek *

*/

include("p_MySql_Connection.php");

$v_Query    =    "SELECT f1 from t1";
          
$v_Result    =    mysql_query($v_Query, $v_RS);

/*
   First loop for one single query
*/
while ($row = mysql_fetch_array($v_Result,MYSQL_NUM)) {
   $v_total = $v_total + $row[1];
}
   echo $v_total;
          

/*
   Retreive data
*/
$v_Re    =    mysql_data_seek($v_Result,0);
if (!$v_Re){
   echo 'MySql data seek Error' .  mysql_error();
}

/*
   Second loop for one single query
*/           
while ($row = mysql_fetch_array($v_Result,MYSQL_NUM)) {
   echo $row[0];

}
arturo_b at hotmail dot com
21-Apr-2005 11:53
hello, this script would be easy to understand for those that are novice in php whose want to understand about this function:

the table "user" have 2 columns "id" and "name".
"user" content:
position 0: "id"=195342481 "name"='Arthur'
position 1: "id"=179154675 "name"='John'
>>position 2<<: "id"=157761949 "name"='April' >>third row<<
position 3: "id"=124492684 "name"='Tammy'
position 4: "id"=191346457 "name"='Mike'

<?php
  mysql_connect
("localhost", "root")
 
mysql_select_db("test");
 
$sql = mysql_query("select * from user");
 
mysql_data_seek($sql, 2);
  echo
"<table border=1>";
  while (
$row = mysql_fetch_row($sql)){
   echo
"<tr><td>$row[0]</td><td>$row[1]</td></tr>";
  }
  echo
"</tabla>";
?>

explanation:
mysql_data_seek move internal result pointer to the third row of table user. Thus mysql_fetch_row will begin by aprils row.
b.steinbrink at g m x dot de
09-Dec-2004 03:09
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
kennethnash1134 at yahoo dot com
26-Mar-2004 04:12
/*here is a nice function for converting a mysql result row set into a 2d array, a time saver if need small data from several rows, saves you from having to do Alot of queries... would be nice to have this built into PHP future versions :) */

// 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
justus dot brugman at 12move dot nl
15-Oct-2000 05:59
So you can use it as the equivalent of the reset() call for an array.
eg.
while ($row = mysql_fetch_object($result_01))
{
// code goes here
  while ($row = mysql_fetch_object($result_02))
  {
   // code goes here.
  }
  mysql_data_seek($result_02,0)
}

Still hoping that there is going to be a better way, like mysql_reset($result).