略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: mysql_data_seek

2024-04-28

mysql_data_seek

(PHP 4, PHP 5)

mysql_data_seek移动内部结果的指针

说明

mysql_data_seek(resource $result, int $row_number): bool

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

范例

示例 #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());
}
/* 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_assoc($result))) {
        continue;
    }

    echo 
$row['last_name'] . ' ' $row['first_name'] . "<br />\n";
}

mysql_free_result($result);
?>

注释

注意:

本扩展自 PHP 5.5.0 起已废弃,并在自 PHP 7.0.0 开始被移除。应使用 MySQLiPDO_MySQL 扩展来替换之。参见 MySQL:选择 API 指南来获取更多信息。用以替代本函数的有:

注意:

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

参见

add a noteadd a note

User Contributed Notes 6 notes

up
7
kennethnash1134 at yahoo dot com
18 years ago
/*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
up
3
saeed at photobookworldwide dot com
10 years ago
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;
?>
up
2
arturo_b at hotmail dot com
17 years ago
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 april

官方地址:https://www.php.net/manual/en/function.mysql-data-seek.php