How to Reset MySQL Pointer Back to the First Row in PHP

How to reset mysql pointer back to the first row in PHP?

use mysql_data_seek. This function will allow you to move the internal result pointer.

How do I reset a PHP pointer for a MySQL resource?

You can use the function mysql_data_seek.

bool mysql_data_seek ( resource $result , int $row_number )

More information can be a found at http://www.php.net/manual/en/function.mysql-data-seek.php.

Getting the first row of the mysql resource string?

Using mysql_fetch_assoc() not only fetches a row, it also moves the internal pointer of the result set to the next row. To reset the result resource to the first row, you need to use mysql_data_seek().

$query = "SELECT * FROM mytable";
$result = mysql_query($query);

$firstrow = mysql_fetch_assoc($result);

// reset the result resource
mysql_data_seek($result, 0);

while($row = mysql_fetch_assoc($result)) {
//display all of them
}

Resetting array pointer in PDO results

Save your results to an array and then loop that array twice.

$pdo = new PDO('mysql:host=' . $host . ';dbname='.$database, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $pdo->prepare('SELECT * FROM mytable WHERE active = 1 ORDER BY name ASC');
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();

$rows = $stmt->fetchAll();

foreach ($rows as $r) {
// first run
}

foreach ($rows as $r) {
// seconds run
}

PHP prepared statement - reset row pointer

I don't think that's possible. But there's not really a compelling reason to try to do it that way anyway.

Keep it simple. Just build an array the first time through.

$rows = array();
//Loop through all rows, adding a third field
while ( $row = db2_fetch_array ( $stmt1 ) ) {
$row[2] = "TEST";
$rows[] = $row;
}
db2_free_result($stmt1);

//Print results
echo "<table border=1>";
foreach($rows as $row) {
echo "<tr>";
echo " <td>" . $row[0] . "</td>";
echo " <td>" . $row[1] . "</td>";
echo " <td>" . $row[2] . "</td>";
echo "</tr>";
}
echo "</table>";

This will, at worst, temporarily double your memory usage while the array is being built.

mysqli ignoring the first row in a table

mysqli not ignoring it but actually you are fetching first row before while loop

$row = mysqli_fetch_assoc($result); //remove this line
while ($row = $result->fetch_assoc()) {
....
}

PHP: cannot loop through mysql rows more than once

This is the error

while ($row = mysql_fetch_array($result)) {
$total_images++;
}

once you fetched the array the array pointer set to the end.

use this

 $total_images=mysql_num_rows($result);

and

$images_to_offset=mysql_num_rows($result);

OR

To reset the position of pointer Use mysql_data_seek(). it moves internal result pointer

How can I loop through a MySQL result set more than once using the mysql_* functions?

This is how you can do it:

$result = mysql_query(/* Your query */);
while($row = mysql_fetch_assoc($result)){
// do whatever here...
}

// set the pointer back to the beginning
mysql_data_seek($result, 0);
while($row = mysql_fetch_assoc($result)){
// do whatever here...
}

However, I would have to say, this doesn't seem the right way to handle this. Why not do the processing within the first loop?

First row of table is not shown in php

Your query is fetching the 1st row, but its not followed by Radio button . So may be you are not able to read it. Comment the below part in your code then you will get all the rows

if($record=mysql_fetch_array($result)){
echo $record['q_id']. ". " ;
echo $record['question']."<br/>";
} else{ echo "error";}

If you still need this part of the code, Please let us know the purpose of it.



Related Topics



Leave a reply



Submit