Jump to content

FETCH_ASSOC / SQLite question


robertlob
Go to solution Solved by 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
Share on other sites

  • Solution

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.