<?php
$query=mysql_query("SELECT model_no,images1 FROM nokia");
echo"<div>";
    while ($img = mysql_fetch_array($query))
    {
        echo $img[0];
        echo "<img src = cms/pages/images/".$img[1].">";
        }
echo"</div>";

?>

what is actually wrong in my code.. its not displaying images($img[1]) but it displays the content 'model_no' ie. $img[0]. plz any sugesstion would be greatly helpful...

Dani AI

Generated

A short, practical checklist and safe examples to diagnose why the model number prints but the image does not.

Common causes: the DB column may contain a filename or a binary BLOB (different handling required); the generated src attribute can be malformed or missing quotes/encoding; the file path on disk may not match the URL; the webserver may return 404 or be blocked by permissions. Several replies in this thread pointed at quoting and API choice (see and ); below are focused diagnostics and minimal, safer examples to try.

Diagnostics to run (quick, in order):

  • Inspect the generated HTML with browser DevTools / View Source and copy the src URL into the address bar — note the HTTP status and response.
  • Confirm what images1 actually contains (filename vs raw binary). Log a small sample to the error log (for example, error_log(...)) and check length/format.
  • If filenames are expected, check the filesystem path and permissions (the webserver user must be able to read the file). Use basename() on the DB value to avoid accidental path injection.
  • If the column is a BLOB, serve it with correct Content-Type headers from a PHP endpoint rather than embedding the raw blob into an img tag.

Safe example (filenames stored in DB, use PDO and encode filename safely):

$stmt = $pdo->query('SELECT model_no, images1 FROM nokia');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $file = rawurlencode(basename($row['images1'])); 
    echo '<img src="/cms/pages/images/' . htmlspecialchars($file, ENT_QUOTES) . '" alt="">';
}

If images are stored as BLOBs, serve them via an endpoint:

/* image.php?id=123 */
$stmt = $pdo->prepare('SELECT mime, data FROM nokia WHERE id = ?');
$stmt->execute([$id]);
$img = $stmt->fetch(PDO::FETCH_ASSOC);
if ($img) {
    header('Content-Type: ' . $img['mime']);
    echo $img['data'];
}

Notes and cautions: prefer storing filenames and letting the webserver serve static files for performance; always use PDO or mysqli with prepared statements (the old mysql_* API is deprecated/removed in newer PHP). For filenames containing spaces or special characters use rawurlencode on the basename before placing it in src, and always wrap attribute values in quotes in the generated HTML.

Recommended Answers

All 5 Replies

Here is a good approach for displaying an image from a MySQL database using PHP.

<?php
$query = $mysqli->query("SELECT model_no,images1 FROM nokia");
$fetch = $query->fetch_object();
$img = $fetch->imgname;
?>
<html>
    <body>
        <img width="100%" height="100%" src="images/<?php echo $img; ?>">
    </body>
</html>
Member Avatar for Member #120589

@sush

I can't see much wrong with your code. Have a look at the underlying html (view source) in your browser. Does the image src attribute show the value you expect it to?

//EDIT

Strike that - you've messed up the double quotes

echo "<img src = cms/pages/images/".$img[1].">";

should be

echo "<img src = 'cms/pages/images/{$img[1]}'>";

yes, like diafol pointed out, change $img[0] to $img[1]

<?php 
$query=mysql_query("SELECT model_no,images1 FROM nokia");
  while ($img = mysql_fetch_array($query))

  @$mod_no=$img['model_no'];
  @$image=$img['images1'];
  echo $mod_no."\n";
  ?>
  <img src="cms/pages/images/<?php echo $image; ?>" width="218" height="172">
  <?php
  }

?>
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.