mysql_data_seek

(PHP 4, PHP 5)

mysql_data_seekMove internal result pointer

Увага

Це розширення застаріле, починаючи з PHP 5.5.0, та вилучене з PHP 7.0.0. Натомість використовуються розширення MySQLi або PDO_MySQL. Докладніше описано у керівництві MySQL: вибір API. Цю функцію можна замінити на:

Опис

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

mysql_data_seek() moves the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number. The next call to a MySQL fetch function, such as mysql_fetch_assoc(), would return that row.

row_number starts at 0. The row_number should be a value in the range from 0 to mysql_num_rows() - 1. However if the result set is empty (mysql_num_rows() == 0), a seek to 0 will fail with an E_WARNING and mysql_data_seek() will return false.

Параметри

result

Результат типу resource для опрацювання, якого повертає функція mysql_query().

row_number

The desired row number of the new result pointer.

Значення, що повертаються

Повертає true у разі успіху або false в разі помилки.

Приклади

Приклад #1 mysql_data_seek() example

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

Примітки

Зауваження:

The function mysql_data_seek() can be used in conjunction only with mysql_query(), not with mysql_unbuffered_query().

Прогляньте також

add a note

User Contributed Notes 4 notes

up
6
kennethnash1134 at yahoo dot com
20 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
2
arturo_b at hotmail dot com
19 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?s row.
up
1
saeed at photobookworldwide dot com
12 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
b.steinbrink at g m x dot de
19 years ago
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
To Top