I am trying to query the image file names from the database using the Id inputted by the user and want to display the images with the resulting file names form the select statement. However the echo function is showing only the broken image icons instead of the image itself. How can I fix this? Thanks!
//$searchValue is the Id inputted by the user in the html page
$result = mysqli_query($con,"SELECT FileName FROM images WHERE Id='$searchValue'");
//$folder is the path where the images are currently stored
$folder = "C:/xampp/htdocs/images/";
while($row = mysqli_fetch_array($result)){
$file = $row["FileName"];
echo '<img src="'.$folder.$file.'"><br />';
}
Related
See Table of report data
The table is as displayed in the image above. I wanted to display all the data including several images for which URLs are saved in the imageURL column separated with a semicolon.
The number of image URLs differs in each columns.
The URLs refer to image path in upload folder as shown below:
InvestigatorsReportApi/uploads/KBC589L-1.jpg;
InvestigatorsReportApi/uploads/KBC589L-2.jpg;
InvestigatorsReportApi/uploads/KBC 589L-3.jpg;
How would I display this in a website?
You should simmply make a query about the URL:
$sql = "SELECT url FROM table";
$result = $conn->query($sql);
After that echo it into the <img> tag src attribute:
while($row = $result->fetch_assoc()) {
echo "<img src='".$row["url"]."'>";
}
And there you have it. The sample code above will print every image in the database.
Just try this
$sql = "SELECT url FROM table";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
foreach(explode(';',$row["url"]) as $image) {
echo "<img src='".$image."'>";
}
}
First fix your table. It is bad. You should let the attributes occupy the first row:
id: name: LOA: regNO: rout..., then
first you need to use explode function for separate your image url
$image=explode(";",$imageURL);
then use foreach loop for display image
foreach($image as $value)
{
<img src='".$value."'/>
}
Firstly I know I shouldn't put the image in a DB but I am still learning.
I want to show the image, where am I going wrong?
In my database I have a column Image that is a BLOB. Small sized image. And I've checked an image array is there.
After I connect the db here is the php
$result = mysql_query("SELECT Image FROM table WHERE ID = 1");
$row = mysql_fetch_array($result);
echo $row["image"];
Any help appreciated.
All I want is just the image to show. Nothing fancy as I will expand on it once I get an image up on the screen.
I think I need an extra step between fetch array to split a variable that contains just the image and to display that. This is where I get lost.
Cheers.
This is the code I use to update the DB (from a form)
$myphoto = $_FILES['MyPhoto'];
$query = mysql_query("UPDATE table SET Image = '$myphoto' WHERE ID = 1") or die(mysql_error());
You need to echo an HTML <image> tag with the src set to your query results to render an image. Use this code:
$result = mysql_query("SELECT Image FROM TABLE WHERE ID = 1");
$row = mysql_fetch_array($result);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/>';
I have the image URL saved in my mySQL database.($imageid).
How can I display images using their respective id? I want to display different images attached to different blog posts,as we see in regular blogs.
Thank you.
Unsure what stage you are, bit you need to query your 'table' for a single result, perhaps the ID? If the column was Imageid then you could display it like below in an array of results.
$con=mysqli_connect("localhost","user","pass","my_db");
$result = mysqli_query($con,"SELECT * FROM table");
while($row = mysqli_fetch_array($result))
{
echo '<img src="' . $row['Imageid'] . '">';
}
mysqli_close($con);
I am building an image uploading website. Images are uploaded to a directory on the server and data such as the filename is stored in a MySql table. I have created a 'gallery' page which displays all the uploaded images as thumbnails. When a user clicks on one of the images, it takes them to 'image.php' page, which will display the image full size and echo information such as the username of the person who uploaded the image etc.
I am unsure as to what would be the correct way of displaying the image. The images in my MySql table have unique ID's which I'm guessing will have to be manipulated in some way, but how do I would I get the ID of the photo that has been clicked on into the 'image.php' MySql query?
Hope this has been explained well enough. Thanks in advance.
gallery.php page... (exclusing database connections etc.)
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM photos");
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
?>
<section class="thumbnails group">
<?php Echo "<img src=http://.../thumbs/tn_".$info['filename'] .">"; }?>
</section>
image.php page...
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM photos WHERE 'id' = ??");
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
?>
<section class="main-image group">
<?php echo "<img src=http://.../images/".$info['filename'] .">"; }?>
</section>
You can simply pass the ID of the image through in the querystring
<img src="/path/to/thumbnail">
In your image.php page, you can retrieve the ID like this (assuming it's an integer):
$imageID = intval( $_GET["image"]);
You should then be able to retrieve the path to the image and display it.
# liquorvicar answer is correct one, there's no ambiguity in his answer! well,
try this;
<?php Echo "<img src=http://...thumbs/tn_".$info['filename'] .">"; }?>
on image.php page
$id=intval($_REQUEST['id'])
now you have id for that specific image show its related info.
SOLVED!
so ive created an image upload page which saves the image to a folder and sends the file name to the DB, ive checked to see if they are actually being added to the DB and folder which they are, but when i call the data to another page to display the images i get broken images.
below is my code to call the images, its some code ive scraped together from various tutorials as they gave me the same problem as im having now.
UPDATE:
ive managed to get the images showing but now im faced with being shown the same image for each row of data called, the id and img_name and right for each row but the image is always the same as the first listed.
UPDATED CODE:
<?php
//connect to database
include ('connect.php');
//save the name of image in table
$query = mysql_query("select * from tbl_img") or die(mysql_error());
//retrieve all image from database and store them in a variable
while ($row = mysql_fetch_assoc($query))
{
$img_name = $row['img'];
}
?>
<?php
include ('connect.php');
$img_id = mysql_query("SELECT * FROM tbl_img");
while ($row = mysql_fetch_assoc($img_id))
{
$id = $row['img_id'];
echo "
$id<br>
$img_name<br>
<img src='http://localhost/testarea/include/site_images/$img_name' />
";
echo "<br><br><br></p>
";
}
?>
If you start the path of image with / you mean an absolute path where / is the DocumentRoot folder (or the directory of virtualhost)
With src ="includes/xxx/image.png" you mean that includes is in the same folder with the php script. If it is not you can use relative path like
src="../includes/xxx/image.png" for example
<?php
include ('connect.php');
$img_id = mysql_query("SELECT * FROM tbl_img");
while ($row = mysql_fetch_assoc($img_id))
{
$img_name = $row['img'];
$id = $row['img_id'];
echo "
$id<br>
$img_name<br>
<img src='http://localhost/testarea/include/site_images/$img_name' />
";
echo "<br><br><br></p>
";
}