mysql_data_seek

2009-06-21 18:15 / no comment / 25 views /

(PHP 4, PHP 5)移动内部结果指针

声明

bool _data_seek ( resource $result , int $row_number )

mysql_data_seek()移动result变量所指的MySQL查询结果的内部指针到指定行。紧跟其后的MySQL取结果函数调用,例如mysql_fetch_assoc(),将会返回这一行。

行数从0开始。参数row_number应该是从0到mysql_num_rows() – 1之间的某一值。然而如果result为空(mysql_num_rows() == 0),到第0行的查找将会失败并产生E_WARNING级的警告,mysql_data_seek()将会返回FALSE。

参数

result
被赋值的查询结果变量。这个值从mysql_query()调用中得来。

row_number
新的结果指针所指向的行数。

返回值

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

例子

<?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);
?>

注意

函数mysql_data_seek()只能和mysql_query()结合起来使用,而不能和mysql_unbuffered_query()联合起来使用。

同样请看

mysql_query() – Send a MySQL query
mysql_num_rows() – Get number of rows in result
mysql_fetch_row() – Get a result row as an enumerated array
mysql_fetch_assoc() – Fetch a result row as an associative array
mysql_fetch_array() – Fetch a result row as an associative array, a numeric array, or both
mysql_fetch_object() – Fetch a result row as an object

Related Posts

  1. mysql_num_rows
  2. PHP中的MySQL函数
  3. mysql_db_query
  4. mysql_query
  5. mysql_affected_rows
  6. mysql_db_name
  7. mysql_connect
  8. mysql_error
  9. mysql_drop_db
  10. mysql_close

Tags: .

Get a Trackback link

No Comments Yet

You can be the first to comment!

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>