Ok, so you have an absolute path to the image. That's not ideal, but we can work around that. The problem is that when you put a
<img> tag in your HTML, the
src attribute has to point to a URL location where the image is found. An absolute, physical location doesn't work.
When the browser gets a
<img> tag, it reads the
src attribute and sends another request to the server, requesting this image. Therefore, the
src attribute has to point to a place where the image can be read via the HTTP server.
If you use this tag in a page at
http://localhost/index.php:
- <img src="C:\xampp\htdocs\HTML\images\image1.jpg">
The browser will try to fetch an image at:
-
http://localhost/C:\xampp\htdocs\HTML\images\image1.jpg
Which will obviously not work.
What you need to do is remove the
https://bytes.com/C:\xampp\htdocs part of the page create a tag that reads:
- <img src="HTML/images/image1.jpg">
This will make the browser request:
-
http://localhost/HTML/images/image1.jpg
Which, if I am understanding you setup correctly, will return the image.
To make this happen in your code, you need to do the following:
- Inside the while loop on line #24, where you echo the row, you need to fetch the image location from the $row into a variable.
- Use the str_replace function to remove the C:\xampp\htdocs part of the location.
- Echo the altered image location into the <img> tag on line #28.
To help you along, here is an example of how this can be done. You will need to adapt this to your own code.
- <?php
-
// Connect to a MySQL database.
-
$dbLink = mysql_connect( 'host', 'user', 'pwd' );
-
mysql_select_db( 'myDb', $dbLink );
-
-
// Fetch images from the database.
-
$sql = "SELECT name, imagelocation FROM myImages";
-
$result = mysql_query( $sql, $dbLink ) or die(mysql_error());
-
-
while($row = mysql_fetch_assoc( $result )) {
-
// Fetch the data for the current image.
-
$name = htmlentities( $row['name'] );
-
$imageLocation = $row['imageLocation'];
-
-
// Remove the junk from the image location
-
$junk = 'C:\\xampp\\htdocs';
-
$imageLocation = str_replace( $junk, '', $imageLocation );
-
-
// Replace \ slashes with / slashes.
-
$imageLocation = str_replace('\\', '/', $imageLocation);
-
-
// Print the <img> tag.
-
echo "<img src=\"{$imageLocation}\" title=\"{$name}\">";
-
}
-
?>