Page 1 of 1

stepping through mysql blob results

Posted: Tue Aug 04, 2009 8:56 am
by iG9
I'm trying to write a script that selects all images from a MySQL table that have a common field and displays them all. This seems like it would work, and does when I use it to step through a different column in the table, but not for the blobs. It only shows the first.

Code: Select all

 
<?php
    include '../lmp_includes/lmp_utilities.php';
    $db = db_connect();
    
    if ($_GET[id]) {
        $id = mysql_real_escape_string($_GET[id]);
        header('Content-type: image/jpg');
        $q = "select * from images where propID = $id";
        $r = mysql_query($q);
        while ($d = mysql_fetch_object($r)) {
            echo $d->image;
        }
    } else {
        echo "Can't do much without an id now can I?";
    }
?>
 
Any ideas greatly appreciated.

Re: stepping through mysql blob results

Posted: Thu Aug 06, 2009 7:58 pm
by iG9
Thanks for the help. Here's how I got it to work.

viewprop.php

Code: Select all

 
<?php
    include '../lmp_includes/lmp_utilities.php';
    $db = db_connect();
    
    if ($_GET[id] || $_GET[offset]) {
        $id = mysql_real_escape_string($_GET[id]);
        $offset = mysql_real_escape_string($_GET[offset]);
        header('Content-type: image/jpg');
        $q = "select * from images where propID = $id limit $offset, 1";
        $r = mysql_query($q);
        $d = mysql_fetch_object($r);
        echo $d->image;
    } else {
        echo "I need both an id and an offset.";
    }
?>
 
proplist.php

Code: Select all

 
<?php
    include '../lmp_includes/lmp_utilities.php';
    $db = db_connect();
    
    $q = "select * from images where propID = $_GET[id]"; //these three lines get num rows
    $r = mysql_query($q);
    $numImages = mysql_num_rows($r);
    $d = mysql_fetch_assoc($r);
    for ($i = 0; $i < $numImages; $i++) {
            echo "<img src=\"viewprop.php?id=1&offset=$i\" height=\"150\" width=\"225\">";
    }
?>
 
I use mysql_num_rows() to get the total number of rows, then a for loop to step through them, using an offset to display only the current image. Hopefully this helps someone. :mrgreen:

Re: stepping through mysql blob results

Posted: Thu Aug 06, 2009 11:56 pm
by iG9
Thanks, duly noted.