Could you answer?

I've a table in mysql database the structure is like:
table name: img_tbl (id, image, size, format, etc..)
image defined as blob datatype inside the table.

i can retrieve single image from mysql database by using the following code.

$sql = "SELECT image,size,format FROM img_tbl";
 
        // the result of the query
        $result = mysql_query($sql) or die("Invalid query: " . mysql_error());
 
        // Header for the image
        header("Content-type: image/jpeg");
        echo mysql_result($result, 0,'imageData');

But i need to display multiple images with other attributes of table from table using php.

Dani AI

Generated

is on the right track: render the list page with normal HTML and load each BLOB via a tiny image endpoint. Use PDO or MySQLi (the old mysql_* API was removed in PHP 7.0), and set the correct MIME type from your format column so browsers display images reliably. (php.net)

<?php // list.php
$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4','user','pass',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$rows = $pdo->query('SELECT id,size,format FROM img_tbl ORDER BY id')->fetchAll(PDO::FETCH_ASSOC);
?>
<ul>
<?php foreach ($rows as $r): ?>
  <li>
    <img src="image.php?id=<?= (int)$r['id'] ?>" alt="Image <?= (int)$r['id'] ?>">
    <small><?= (int)$r['size'] ?> bytes, <?= htmlspecialchars($r['format']) ?></small>
  </li>
<?php endforeach; ?>
</ul>
<?php // image.php
$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4','user','pass');
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$id) { http_response_code(404); exit; }

$stmt = $pdo->prepare('SELECT image,format FROM img_tbl WHERE id = ?');
$stmt->execute([$id]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) { http_response_code(404); exit; }

$ext = strtolower($row['format'] ?? '');
$mime = ($ext === 'png') ? 'image/png' :
        (($ext === 'gif') ? 'image/gif' :
        (($ext === 'webp') ? 'image/webp' : 'image/jpeg'));

header('Content-Type: '.$mime);
header('X-Content-Type-Options: nosniff');
header('Cache-Control: public, max-age=86400');
header('Content-Length: '.strlen($row['image']));
echo $row['image'];
exit;

That browser message usually means the HTTP headers were wrong or any output (even one space/BOM) was sent before them. Make sure nothing echoes before the headers, and, if you use output buffering, clear it before sending the image (for example, call ob_clean() only when a buffer is active). Also double‑check you are reading the correct column name and that the BLOB type is large enough for the file. (php.net)

Once your SELECT filters by category, just print <img src="image.php?id=..."> inside your carousel’s markup. If you are using Bootstrap 5, follow the component’s structure and it will cycle through those dynamic images. (getbootstrap.com)

Recommended Answers

All 4 Replies

write two php files, one for showing all other data

file 1.php
1)fetch data from mysql with primary key
2) show all columns
3) <img src='file2.php?imgid=currid'>

file2.php //it will contain code you have written to show image in your post above, but it will use one get parameter to load specific single image.

$sql = "SELECT image,size,format FROM img_tbl where record_id='{$_GET['imgid']}'";
 
        // the result of the query
        $result = mysql_query($sql) or die("Invalid query: " . mysql_error());
 
        // Header for the image
        header("Content-type: image/jpeg");
        echo mysql_result($result, 0,'imageData');
commented: Thank You. this code was really helpful for me and for other who has same problem. +1

dear untrived,
thank you very much for given such a usefull logic.

filename:display.php

<?php

    $con=@mysql_connect('localhost','root','')or die(mysql_error());

    $sdb=@mysql_select_db('interview', $con)or die(mysql_error());

    $sql = "SELECT image FROM prodlist where id='{$_GET["id"]}'";

  // the result of the query
    $result = mysql_query($sql) or die("Invalid query: " . mysql_error());

  // Header for the image
    header("Content-type: image/jpeg");
    echo mysql_result($result, 0,'image');

?>

when i need to display a image, it works perfectly. when i updated image, change image to mysql table successfully, but image not displayed. It shows "The image cannot be displayed, it contains errors". Help me.
mysql table successfully,

hey there i have a page where i want to display a carousel with multiple images
based on category selection plz help me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.