mysql_data_seek
(PHP 4, PHP 5, PECL mysql:1.0)
mysql_data_seek — Перемещает внутренний указатель в результате запроса
Описание
Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки.
mysql_data_seek() перемещает внутренний указатель в результате запроса к ряду с указанным номером. Следующий вызов mysql_fetch_row() вернёт именно его.
Параметр Row_number должен быть значением от 0 до mysql_num_rows - 1.
Замечание: Функция mysql_data_seek() может быть использована только с mysql_query(), но не с mysql_unbuffered_query().
Пример #1 Пример использования mysql_data_seek()
<?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());
/* получение рядов в обратном порядке */
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().
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с базами данных
- Расширения для работы с базами данных отдельных производителей
- MySQL Drivers and Plugins
- Оригинальное API MySQL
- mysql_affected_rows
- mysql_client_encoding
- mysql_close
- mysql_connect
- mysql_create_db
- mysql_data_seek
- mysql_db_name
- mysql_db_query
- mysql_drop_db
- mysql_errno
- mysql_error
- mysql_escape_string
- mysql_fetch_array
- mysql_fetch_assoc
- mysql_fetch_field
- mysql_fetch_lengths
- mysql_fetch_object
- mysql_fetch_row
- mysql_field_flags
- mysql_field_len
- mysql_field_name
- mysql_field_seek
- mysql_field_table
- mysql_field_type
- mysql_free_result
- mysql_get_client_info
- mysql_get_host_info
- mysql_get_proto_info
- mysql_get_server_info
- mysql_info
- mysql_insert_id
- mysql_list_dbs
- mysql_list_fields
- mysql_list_processes
- mysql_list_tables
- mysql_num_fields
- mysql_num_rows
- mysql_pconnect
- mysql_ping
- mysql_query
- mysql_real_escape_string
- mysql_result
- mysql_select_db
- mysql_set_charset
- mysql_stat
- mysql_tablename
- mysql_thread_id
- mysql_unbuffered_query
Коментарии
/*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
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
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?s row.
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;
?>