Home > Backend Development > PHP Tutorial > PHP code using mysql_data_seek to move the pointer to the initial position of the data set

PHP code using mysql_data_seek to move the pointer to the initial position of the data set

WBOY
Release: 2016-07-25 09:03:34
Original
1132 people have browsed it
  1. // Start snipit 1
  2. $sql = "SELECT * from ";
  3. $result = mysql_query($sql);
  4. while ($row = mysql_fetch_assoc($result)) {
  5. // do stuff with $row
  6. }
  7. mysql_data_seek($result, 0); //The key is here
  8. while ($row = mysql_fetch_assoc($result)) {
  9. // do other stuff with $row
  10. }
  11. ?>
  12. Copy code

    Definition and usage

    mysql_data_seek() function moves the pointer of the internal result.

    Grammar mysql_data_seek(data,row) parameter description data required. Returns a result set of type resource. This result set is obtained from a call to mysql_query(). row required. The number of rows in the new result set pointer you want to set. 0 indicates the first record.

    Instructions mysql_data_seek() moves the row pointer within the MySQL result specified by the data parameter to the specified row number. A subsequent call to mysql_fetch_row() will return that row. row starts from 0. The value range of row should be from 0 to mysql_num_rows - 1. But if the result set is empty (mysql_num_rows() == 0), moving the pointer to 0 will fail with an E_WARNING level error, and mysql_data_seek() will return false.

    Return value Returns true if successful, false if failed.

    Tips and Notes Note: mysql_data_seek() can only be used with mysql_query(), not mysql_unbuffered_query().

    1. $con = mysql_connect("localhost", "hello", "321");
    2. if (!$con)
    3. {
    4. die('Could not connect: ' . mysql_error( ));
    5. }
    6. $db_selected = mysql_select_db("test_db",$con);
    7. $sql = "SELECT * from Person";
    8. $result = mysql_query($sql,$con);
    9. print_r(mysql_fetch_row($result ); Array ( [0] => Adams [1] => John [2] => London )
    10. Array ( [0] => Carter [1] => Thomas [2] => Beijing )

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template