(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
。
成功时返回 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());
}
/* 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);
?>
Note: Suggested alternatives
Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API for more information.
Alternatives to this function include:
- mysqli_data_seek()
PDO::FETCH_ORI_ABS
Note:
mysql_data_seek() 只能和 mysql_query() 结合起来使用,而不能用于 mysql_unbuffered_query()。