Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

mysql_data_seek

(PHP 3, PHP 4, PHP 5)

mysql_data_seek -- Перемещает внутренний указатель в результате запроса

Описание

bool mysql_data_seek ( resource result_identifier, int row_number )

Возвращает 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().



mysql_db_name> <mysql_create_db
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
mysql_data_seek
Guy Gordon
27-Jun-2007 01:26
I needed to "peek" at the next record in order to see if fetching it would go too far.  So I want to do a fetch, followed by seek(-1).
 
I could find no function to move the internal row pointer relative to it's current position, or to retrieve it as a row number as required by mysql_data_seek().  This limits the function's usefulness to resetting the row to 0, unless you track the row number yourself.

If you use a While loop to step through the results, you can increment a tracking index at the bottom of the loop.  But be sure never to use Continue; which would bypass your index.  And document this restriction for the person who needs to maintain your code.  It's probably better to use a For loop, which makes the index explicit. 

In either case be sure to range check the index when you manipulate it.  E.G. When I "peek" at the next record I must check for index>=count (end of data).  Or if I decrement the index, make sure it does not go negative.  Again, document why you are coding it this way, so the next programmer doesn't "correct" the inelegant code.
30-May-2006 01:52
A helpful note about the 'resource' data type.

Since the 'resource' variable is pointing to a row in a result set at any given time, you can think of it as being passed to this function by reference every time you pass it or assign it to a variable.

<?

$sql = "SELECT * from <table>";
$result = mysql_query($sql);
$temp_result = $result;

while ($row = mysql_fetch_assoc($temp_result)) {
    // do stuff with $row
}

while ($row = mysql_fetch_assoc($result)) {
    // This code will never run because the 'resource' variable is pointing past the end of the result set,
    // even though it was *not* assigned by reference to $result2.
}

?>

Therefore, the following snipits are functionally identical:

<?

// Start snipit 1

$sql = "SELECT * from <table>";
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {
    // do stuff with $row
}

mysql_data_seek($result, 0);

while ($row = mysql_fetch_assoc($result)) {
    // do other stuff with $row
}

// Start snipit 2

$sql = "SELECT * from <table>";
$result = mysql_query($sql);
$temp_result = $result;

while ($row = mysql_fetch_assoc($temp_result)) {
    // do stuff with $row
}

mysql_data_seek($result, 0);

while ($row = mysql_fetch_assoc($temp_result)) {
    // do other stuff with $row
}

?>
jonybd at yahoo dot com
27-Jun-2005 05:40
/*
    helpfull for real time databases query
    - Query one time
    - Retreive data twice from the same query
    - mysql_data_seek *

*/

include("p_MySql_Connection.php");

$v_Query    =     "SELECT f1 from t1";
           
$v_Result     =     mysql_query($v_Query, $v_RS);

/*
    First loop for one single query
*/
while ($row = mysql_fetch_array($v_Result,MYSQL_NUM)) {
    $v_total = $v_total + $row[1];
}
    echo $v_total;
           

/*
    Retreive data
*/
$v_Re     =     mysql_data_seek($v_Result,0);
if (!$v_Re){
    echo 'MySql data seek Error' .  mysql_error();
}

/*
    Second loop for one single query
*/           
while ($row = mysql_fetch_array($v_Result,MYSQL_NUM)) {
    echo $row[0];

}
arturo_b at hotmail dot com
20-Apr-2005 08:53
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
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571