Displaying Image from image uploader - php

im trying to display image from database by calling filename of the image.
here's my code.
$fname=$row['user'];
$query1=mysql_query("select * from user where username = '$fname'");
$q_pix =mysql_fetch_array($query1);
$id=$row['id'];
$date=$row['date'];
$message=$row['message'];
$lugar=$row['location'];
<img src=".$row['location']." height=\"120\" width=\"120\">
Undefined index: location.

First of all print both the variables $row and $q_pix and check in which you have your location field.
Try to use $q_pix variable in img src if it has -
<img src=".$q_pix['location']." height=\"120\" width=\"120\">

<?php
$fname = $row['user'];
$query1 = mysql_query("select * from user where username = '$fname'");
$q_pix = mysql_fetch_array($query1);
?>
Here you are getting values from database to the variable $q_pix, not to $row.
So retrieve values from $q_pix instead of $row. (Print both $q_pix and $row to make sure they are getting values).
<?php
$id = $q_pix['id'];
$date = $q_pix['date'];
$message = $q_pix['message'];
$lugar = $q_pix['location'];
?>
<img src="<?php echo $q_pix['location']; ?>" height=\"120\" width=\"120\">
NOTE : Make sure your $q_pix['location'] value includes the directory stucture too with the image name.

Related

image display from database in php

i want to display image from database and below is my code where empid is the value of textbox, it's showing the image name stored in database but i am not able to echo this picture. anyone can help in this regard.
<?php
include('connect.php');
$result = $db->prepare("SELECT image FROM info WHERE empid= $empid");
$result->bindParam('$empid', $empid);
$result->execute();
for($i=0; $rows = $result->fetch(); $i++){
echo $rows['image'];
}
?>
Change this line.
$result = $db->prepare("SELECT image FROM info WHERE empid= '". $empid ."'");
its only show you the filename that you stored in your db . if you want to show image then first add folder location with image name and pass into the <img src="excatImageLocation">
Example -
$yourExactPath = "YourImageUploadFolderLocation".$rows['image']; // img/yourFilename.jpg;
echo "<img src='$yourExactPath' />
You either concatenate the id onto the text string containing the query or use a parameter place holder and then bind a value to it. Not both, as you were doing.
The most secure way is to use parameters.
<?php
include('connect.php');
$result = $db->prepare("SELECT image FROM info WHERE empid= :empid");
$result->bindParam(':empid', $empid, , PDO::PARAM_INT);
$result->execute();
while ($row = $result->fetch(PDO::FETCH_ASSOC)){
echo $row['image'];
}
?>

retrieve image from database using mysqli php 7

I am trying to retrieve a png image file from the database
Here is the call from within the <img> tag inside body:
<img src="..\BankLogin\man.php?id=2" style="width:128px;height:150px">
Here's the man.php file:
<?php
$link = mysqli_connect("localhost","root","","images");
$imgId = $_GET['id'];
if (!empty($imgId)) {
$sqliCommand = "SELECT image FROM images WHERE id = $imgId";
$result = mysqli_query($sqliCommand,$link);
$row = mysqli_fetch_assoc($result);
mysqli_close($link);
header("Content-type: image/png");
echo $row['image'];
}
?>
On running the code i just get an image frame with an 'unloaded image'(am i saying it correct?).I am pretty sure something is wrong in the man.php file, maybe in echo $row['image']. I am not sure how to go about making it right. Any help with this would be great.
The function mysqli_close should be called after the image data is echoed. This is because it destroys the result sets.
Also please fix the SQL Injection vulnerability:
$imgId = (int)$_GET['id'];
If you want to retrieve the image which is stored as BLOB type in phpmyadmin you have to echo it as follows.
echo '<img src="data:image/jpeg;base64,'.base64_encode( $rows['image'] ).'"/>'
Example:
To Retrieve the BLOB image from the DB you have to do like this.
<?php
$db = mysqli_connect("localhost","root","","dbname"); //keep your db name
$query = "SELECT * FROM image WHERE id = $id";
$sth = $db->query($query);
$fetch=$sth->fetch_assoc();
echo '<img src="data:image/jpeg;base64,'.base64_encode( $fetch['image'] ).'"/>';
?>
For Inserting the image you need to follow the procedure like this So that if you encode it as base 64 you can retrieve the image perfectly without any error.
<?php
$conn = mysqli_connect("localhost","root","","DbName"); //keep your db name
$single_image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
//U have to keep your DB table column name for insertion. I keep image type Blob.
$query = "INSERT INTO image (image) VALUES('".$single_image."')";
$SQL = mysqli_query($conn, $query);
?>

image now showing when called from database

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>
";
}

MYSQL query optimisation

the problem I am having I will describe it as best as I can....The user chooses three selections and the image appears on the screen, and the selections they made are sent and stored in the database, the PHP function below then finds the image for the selection and returns them as variables so the latest selection they made and the images corresponding to the selection is still displayed in the placeholders after the page is refreshed, the SQL code I have below I want to incorporate into one Query but I need it so where the data for $driver1 is found it is placed into the $image1 variable and the same for $driver2 into $image2, and $driver3 to $img3 so I can return them to the page and echo the correct images in the correct placeholders. The images for the selections they made need to appear in the placeholders that they originally appeared in. It works fine as it is now, but I am sure that the SQL querys can be shortened to one? to save lines of code.
The code I have posted below is the main important bits so you can hopefully understand what I am asking.
Any help would be great, thank you.
//HTML CODE
<?php
list($img1, $img2, $img3) = checkteam();
?>
<img onclick="return removedriver1(this)" id="advert" src="images/delete.gif" border="0"/>
<img id="placeholder1" src="<?php echo "$img1";?>" alt="" />
<img onclick="return removedriver2(this)" id="advert src="images/delete.gif" border="0"/>
<img id="placeholder2" src="<?php echo "$img2";?>" alt="" />
<img onclick="return removedriver3(this)" id="advert" src="images/delete.gif" border="0"/>
<img id="placeholder3" src="<?php echo "$img3";?>" alt="" />
//PHP CODE
function checkteam(){
$sql="SELECT image FROM drivers WHERE drivers_id = '$driver1'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$image1=$row[image];
$sql="SELECT image FROM drivers WHERE drivers_id = '$driver2'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$image2=$row[image];
$sql="SELECT image FROM drivers WHERE drivers_id = '$driver3'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
$image3=$row[image];
return array ($image1, $image2, $image3);
}
The best way to do this in my opinion is to stick them all into an associative array:
$sql = "SELECT drivers_id, image FROM drivers WHERE drivers_id IN ($driver1, $driver2, $driver3)";
$result = mysql_query($sql);
$images = array();
while (($row = mysql_fetch_assoc($result)) {
$images[$row["drivers_id"]] = $row["image"];
}
You can then print out the appropriate image with $images[$driver1] etc.
You could do one query only:
$sql = "SELECT drivers_id id, image FROM drivers WHERE drivers_id in ($driver1, $driver2, $driver3)";
You would need to myqsl_fetch_array three times, of course.
while ($row = mysql_fetch){
if ($driver1 == $row['id'])
$image1 = $row['image'];
else if ($driver2 == $row['id'])
$image2 = $row['image'];
else if ($driver3 == $row['id'])
$image3 = $row['image'];
}
select drivers_id, image from drivers where drivers_id in ($driver1, $driver2, $driver3)
or
select drivers_id, image from drivers where drivers_id = $driver1 or drivers_id = $driver2 or drivers_id = $driver3
loop through the results and match drivers_id to echo the correct image in the correct placeholder.

Getting information from image using mysql database

I have a home page where database value are showing (e.g. a image and few information)
My problem is whenever i click on the image it jumps to another page and show all the pictures that are store in database,but i want the clicked one.
Here is my 1st page home_page.php code
<?php
$db = mysqli_connect("localhost", "root", "", "registration_data");
$sql = "SELECT * FROM add_data";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_array($result)){
$image = $row['image'];
echo "<div id='img_div'>";
?><?php echo "<img src='images/".$row['image']."' >";?><?php
echo "<p>Description: ".$row['add_description']." </p>";
echo "<p>Cetegory: ".$row['catagory']. "</p>";
echo "</div>";
}
?>
And this is my second page where i want to show only clicked image information.(Details.php)
<?php
$image = intval($_GET["image"]);
$db = mysqli_connect("localhost", "root", "", "registration_data");
$sql = "SELECT * FROM add_data where 'id' = $image ";
$result = mysqli_query($db,$sql);
while($row = mysqli_fetch_assoc($result)){
$imageShow = $row['image'];
print $imageShow;
}
?>
So, if your initial page for calling the image is something like:
<body>
<img src="getImage.php?**id=1**" width="175" height="200" />
</body>
Then getImage.php is
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("dvddb");
$sql = "SELECT dvdimage FROM dvd WHERE id=**$id"**;
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['dvdimage'];
?>
You're currently passing row, rather than ID.
Your table isn't telling anyone what cell is which, you should be using a primary key as a reference id.
so your SQL should select the table colums and assign them to values within each row, then you pass in the query to pull back the image with a certain primary key.
i.e. row is a PHP value, you need to extract and reference a DB value, a primary key. i.e. ImageID.

Categories