PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
<?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?";
}
?>
<?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.";
}
?>
<?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.