Hi, everyone!
I need some urgent help with displaying images in a web page from a database. I've created the table picture in phpmydamin and it has these columns: id_picture( wich is the primary key), name, description and the pic_url ( wich contains the url path of the image for example: images/pic_1.jpeg). For now I just want to display the image, but instead it displays the url path as a text that is located in the pic_url column. This is the code I'm using:

<?php
mysql_connect("localhost","root","")
or die('Could not connect: ' . mysql_error());

mysql_select_db("art");
$result = mysql_query("SELECT * FROM picture");

?>

<?php

while($rows=mysql_fetch_array($result)){  
    $pictur=$rows["id_picture"];
    ?>

  <?php
     echo"<a > $rows[pic_url]</a>";
  ?>


<?php
}
mysql_close();
?>

Dani AI

Generated

A few clarifications and a compact, safer approach.

stored a filesystem/URL path in pic_url, which is fine. was on the right track by suggesting an <img> tag; correctly noted the anchor needs an href; 's idea of storing full HTML in the DB is fragile and not recommended. The reason the page showed the URL as text is that the value was echoed as raw text rather than used as an src attribute for an image element.

A simple, modern pattern: fetch name and pic_url from the DB with PDO or MySQLi, escape output, confirm the file exists on the server (to avoid broken images), and emit an <img> tag using the pic_url value. Escape with htmlspecialchars to prevent XSS. Prefer storing only the filename/path in the DB (not HTML). Avoid the old mysql_* extension — use PDO or MySQLi and prepared statements for real projects.

Example (illustrative) workflow:

<?php
// open DB with PDO (use credentials and error handling in real code)
$stmt = $pdo->query("SELECT name,pic_url FROM picture");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $src = htmlspecialchars($row['pic_url'], ENT_QUOTES, 'UTF-8');
    $alt = htmlspecialchars($row['name'] ?? '', ENT_QUOTES, 'UTF-8');
    // optional: check filesystem with DOCUMENT_ROOT to avoid 404s
    echo '<img src="'.$src.'" alt="'.$alt.'">';
}
?>

Notes and cautions: if images are stored as BLOBs, serve them via a dedicated script that sends the correct Content-Type header rather than embedding raw BLOBs into page HTML. Ensure file permissions and path references are correct (relative vs absolute). Use browser devtools to inspect the generated <img> tag and network requests if an image does not appear.

Recommended Answers

All 5 Replies

Member Avatar for Member #949455

For now I just want to display the image, but instead it displays the url path as a text that is located in the pic_url column. This is the code I'm using:

Base on your code. I assume the image is in your database. You can just echo it out like this:

<img src="<?php echo $rows["id_picture"]; ?>" alt="Image from DB" />
commented: I agree with you ;) +0
Member Avatar for Member #120589

AS LM says. ALso you use:

echo"<a > $rows[pic_url]</a>";

This needs a href attribute.

I've always just stored the href themselves in the database and then ran the output as such:

<? php echo "<img id='idName' href='".$variableName."'/>"; ?>

Another option is to store the whole img tag in your database then doing a simle echo on the page in quotes like so :

<?php echo "'".$variable."'"; ?> 
Member Avatar for Member #120589

I've always just stored the href themselves in the database and then ran the output as such

I think you mean src with an img tag and href with an anchor tag?

Another option is to store the whole img tag in your database then doing a simle echo on the page in quotes like so :

I don't think you need anything as complicated:

<?php echo $variable; ?>

No need for all those quotes and concatenators.

Thank you so much for your help :) I'll try them and let you know :)

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.