Hi there i'm trying to display an image on a webpage.

The image's URL is stored in a mysql database.

i just want to output the image to the display via PHP.

Any ideas? Thanks

Dani AI

Generated

There are three common situations here: the DB field holds a plain URL/path, it holds binary image data (BLOB), or it contains an HTML snippet (an anchor or img tag). is correct for the plain-URL case; was right to ask about BLOBs. The approach depends on which one you actually have.

For a stored URL or filesystem path, fetch it with a prepared query and output it into the src attribute after escaping to prevent XSS. Use modern APIs (PDO or mysqli) rather than the old mysql_* functions. Example pattern (PDO):

$stmt = $pdo->prepare('SELECT image_url FROM gallery WHERE id = ?');
$stmt->execute([$id]);
$url = $stmt->fetchColumn();
if ($url) {
    echo '<img src="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8') . '" alt="">';
}

If the image is stored as a BLOB, serve it through a dedicated script (for example image.php?id=123) that returns the correct Content-Type header and echoes the binary. Store the MIME type with the blob if possible so the browser knows how to render it.

If the DB contains an HTML fragment (an <a> or <img> tag), do not blindly echo it unless it is fully trusted. Prefer extracting the URL and rebuilding the markup. A safe way is to parse the stored HTML (for example with PHP's DOMDocument), pull the src or href you need, validate it, then output an escaped img tag. Always validate URLs (absolute vs relative), check file permissions for local paths, and consider caching headers for served images. For reference, see the PHP docs on htmlspecialchars and DOMDocument for safe parsing.

Recommended Answers

All 3 Replies

You have stored the image as some sort of Binary Large Object (BLOB) and want to return it to a browser as a file?

Just want to make sure I understand the problem before I explain how to do this.

If it is only an image URL, you can simply concatenate into an <img> tag after using the mysql query e.g. echo "<img src=\"". $row['image_url'] ."\" alt=\"\" />";

<?php
$query = "SELECT image_url FROM gallery WHERE id = ". mysql_real_escape_string($id); 
	 
$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result) or die(mysql_error());

echo "<img src=\"". $row['image_url'] ."\" alt=\"\" />";
?>

Martin5211 is right. If you post some code il be happy to code it in for you.
Hope this helps

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.