Jump to content

FETCH_ASSOC / SQLite question


robertlob

Recommended Posts

The following snippit should retrieve 10 images stored in an SQLite database table. But for some reason that mystifies me, it only retrieves 9. (Or one less than the number of images that are actually stored on the table):

___________

# retrieve photos for this property
        
        $query = "SELECT * FROM binary_data where propID = $prop_id";
        $result = $db->query($query);
        $row = $result->fetch(PDO::FETCH_ASSOC);
        
        
        
        # save each image data to file
        foreach($result as $row) {
        $filename = $row['filename'];
        $image = $row['bin_data'];
        file_put_contents($filename, $image);
        }
        ?>

_____________

It saves the ones it gets to the temporary files as it should, and they appear on the webpage as they should. But one is always missing. Any suggestions/corrections appreciated.

-Robert

 

 

Link to comment
https://forums.phpfreaks.com/topic/288862-fetch_assoc-sqlite-question/
Share on other sites

I found a working solution:

Removed this line from my code...

$row = $result->fetch(PDO::FETCH_ASSOC);

and it seems to solve the problem. The is a similar Q in the FAQ section, although it regards mySQL instead of SQLite.

"The function returns the next row in the result set and advances the internal pointer. So before your loop runs, you're already pointing at the second row in the result set." Removing that line allows it to start at the first row."

 

-Robert

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.