Asked  7 Months ago    Answers:  5   Viewed   89 times
$data=$stmt->fetchAll(); //Dumping the data shows the result. It is also setting the cursor at the end

//Does not enters loop
//If fetchAll() removes it work as usual

I know It dont need to fetch data twice. But my main question is How to reset cursor position in PDO?



AFAIK there is no possibility to reset cursor position with PDO - that might something to do with compatibility with some databases, that don't support resetting internal cursors.

If you want to iterate twice over the results, fetch it to the array and iterate over this array:

$results = $stmt->fetchAll();  
foreach($results as $row) {
    // first

foreach($results as $row) {
    // second

Edit Some databases support scrollable cursors. To use that, add PDO::CURSOR_SCROLL flag to prepare method (see examples at PDOFetch documentation page). But that only adds possibility to move forward or backward, not rewind completely. Also, not all databases support that type of cursor (e.g. MySQL doesn't).

Wednesday, March 31, 2021
answered 7 Months ago

It seems that PDO::MYSQL_ATTR_FOUND_ROWS is a mysql connection option. Thus, it works only as PDO connection option as well. So, set it up this way

$opt  = array(
    // you may wish to set other options as well
$this->_db = new PDO($dsn,DB_USER,DB_PASS,$opt);
Wednesday, March 31, 2021
answered 7 Months ago

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

$pdo = new PDO('mysql:host=' . $host . ';dbname='.$database, $username, $password);

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

$rows = $stmt->fetchAll();

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

foreach ($rows as $r) {
    // seconds run
Wednesday, June 2, 2021
answered 5 Months ago

Not jQuery, but just Javascript...

var position = window.getSelection().getRangeAt(0).startOffset;
Saturday, June 5, 2021
answered 5 Months ago

Your PDO is configured to emulate prepared queries, whereas mysqli is using true prepared queries.

The prepared query binds the string ''1'' as an integer parameter value. PHP coerces it to an integer using something like intval(). Any string with non-numeric leading characters is interpreted as 0 by PHP, so the parameter value sent after prepare is the value 0.

The fake prepared query uses string interpolation (instead of binding) to add the string ''1'' into the SQL query before MySQL parses it. But the result is similar, because SQL also treats a string with non-numeric leading characters in an integer context as the value 0.

The only difference is what ends up in the general query log when the parameter is bound before prepare versus after prepare.

You can also make PDO use real prepared queries, so it should act just like mysqli in this case:

$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

PS: This may demonstrate a good reason why it's customary to start id values at 1 instead of 0.

Wednesday, August 18, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :