Hello,

I need help on this...

I try to insert image from mysql into my php table.... but the output inside the image table are unknown characters

Sm.l1ZQmMԽok*5l0P9fI$A3tu4ZJǽ_t/&[{Ro; :I%yҩ?u\t_Ǡ]N>Bz9ҥ=hƬկdw~hY4Ef%MC 1IP@zP&rj֩k޺8apbSMytCoQ$?ZF*S9حRxh9$J \m,[6^CctqU%K$%84leniX\B!s9n;1F~t䎪:E+7R["! 1ZݞzQ>(zSxѹ 5⺞mG%֦k/9]_4!4}hI

Dani AI

Generated

Those "unknown characters" are the raw binary bytes of your image being printed into the HTML document. The fix is to have the <img> tag request a script that outputs only the image bytes with the correct headers. Also note in the example above the src is missing the equals sign after id; it should be getImage.php?id=<?php echo $row['id']; ?>.

If you stick with this approach, switch to prepared statements and send the actual MIME type. Storing a mime_type column alongside a BLOB avoids always forcing JPEG.

<?php
// get-image.php (PDO example)
require __DIR__ . '/db.php'; // creates $pdo = new PDO(...)

$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$id) { http_response_code(400); exit; }

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

header('Content-Type: ' . $row['mime_type']);
header('Content-Length: ' . strlen($row['data']));
header('Cache-Control: public, max-age=86400');
echo $row['data'];
exit;

Practical tips:

  • Ensure no whitespace or BOM is sent before header() and the binary output, or the image will be corrupted.
  • Validate and parameterize the id to prevent SQL injection.
  • Consider storing files on disk and only paths in the DB for large galleries; it simplifies caching and reduces DB load.
  • The old mysql_* API used above is removed in PHP 7+. Use PDO or MySQLi instead; see PHP PDO prepared statements and header(). For BLOB types, see MySQL’s BLOB data type docs.

Recommended Answers

All 3 Replies

You have to write a separate PHP file to get the image. In that file you have to fetch the image data from the data base and send it back
Following is an example (getImage.php)

<?php
        // getImage.php file

	// Connect to the database here 
	 
	$result = mysql_query("SELECT image FROM gallery WHERE id='".$_GET['id']."'");
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	header("Content-type: image/jpeg");
	echo $row['image'];
?>

Then you can pass the id and fetch the image. So in your code, in the place you want to display the image, call getImage.php?id=<id_value> in <img> tag's src attribute

// Inside the while loop of yours
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td><img src='getImage.php?id".$row['id']."'/></td>";
echo "<td>".$row['time']."</td>";
echo "</tr>";

oh ... thank you so much man !.. it works.. :)

Welcome :) :)

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.