Home > Backend Development > PHP Tutorial > Move the pointer to the initial position of the data set in PHP_PHP tutorial

Move the pointer to the initial position of the data set in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:44:29
Original
1026 people have browsed it

In php, we need to return the initial position of the data set through the mysql_data_seek function. See the code below

The code is as follows
 代码如下 复制代码

// Start snipit 1

$sql = "SELECT * from

";
$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
}
?>

Copy code



// Start snippet 1

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


while ($row = mysql_fetch_assoc($result)) {

                // do stuff with $row

}

mysql_data_seek($result, 0); //The key is here

while ($row = mysql_fetch_assoc($result)) {

                // do other stuff with $row

}
?>

Definition and usage

The mysql_data_seek() function moves the internal result pointer.

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. Description mysql_data_seek() moves the row pointer within the MySQL result specified by the data parameter to the specified row number. Then calling 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.
http://www.bkjia.com/PHPjc/633101.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633101.htmlTechArticleIn php, we need to return the initial position of the data set, which can be achieved by the mysql_data_seek function. See the code below. Copy the code as follows? // Start snipit 1 $sql = SELECT * from table; $result =...
source:php.cn
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