Hey guys, so I have been trying to get images to appear with an SQL query using PHP.

My table has a column called picture, it is datatype nvarchar50. I have a folder in my project called images and in there an image called 4.png. So in the field of my picture column, I have the string, images/4.png.
In my query, I return data from a query and am trying to make an image show with this data, here is my query output code:

while( $row = sqlsrv_fetch_array ( $result, SQLSRV_FETCH_ASSOC))
														{
															echo 'ID number: '.$row['productID'];
															echo '<br/> category: '.$row['category'];
															echo '<br/> product name: '.$row['productName'];
															echo '<br/> Range: '.$row['range'];
															echo '<br/> description: '.$row['description'];
															echo '<br/> size: '.$row['size'];
															echo '<br/> colour: '.$row['colour'];
															echo '<br/> availible: '.$row['available'];
															echo '<br/> price: '.$row['price'];
															echo '<br/> picture: <img  src="'.['picture'].'">;
														
														}

This works, but the last line doesn't, how can I load an image in my query from my project folder, I do not want to store images in the database, just strings linking to the images, can anyone please help me out?

Many thanks!

Dani AI

Generated

Nice quick fix from @diafol — the problem was just how the src string was built. A few follow-ups that make the result more robust and safer for production:

  • Always escape DB content before inserting into HTML to avoid XSS.
  • Prefer web-root–absolute paths (start with /) so pages in subfolders still resolve images.
  • Verify the file exists on disk (and is readable) before printing the tag; if missing, show a placeholder.
  • Store only a filename or normalized relative path in the DB (e.g., 4.png or images/4.png) and compose the full URL in PHP so you can change folder structure later.

Example helper you can drop into your code:

function render_image($relPath, $alt = 'Image') {
    $relPath = ltrim($relPath, '/');
    $fsPath = rtrim($_SERVER['DOCUMENT_ROOT'], DIRECTORY_SEPARATOR)
              . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $relPath);

    if (is_file($fsPath) && is_readable($fsPath)) {
        $url = '/' . $relPath; 
        echo '<img src="' . htmlspecialchars($url, ENT_QUOTES, 'UTF-8')
             . '" alt="' . htmlspecialchars($alt, ENT_QUOTES, 'UTF-8') . '" />';
    } else {
        echo '<img src="/images/missing.png" alt="missing image" />';
    }
}

Quick troubleshooting: view page source and paste the src into the browser, check file permissions, and confirm your NVARCHAR length is enough for the paths you store. — glad it worked; these tips will help keep it safe and reliable.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589
<img  src="'.$row['picture'].'" />;

It works now thanks a lot!

Member Avatar for Member #120589

OK, mark as solved. :)

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.