image display from database in php - 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'];
}
?>

Related

php a href display data from mysql database and link it to a new page

I have a php & mysql search script which is working, after result displayed users can click below link and it will open a new page and display more mysql info, i am stuck with linking the ahref link ($field2name) to the new page (getprofile.php).
Now the problem is getprofile.php showed nothing and displayed 0 results, however i tried to put static data in getprofile.php it is working properly and can show profile data from mysql, can anyone enlighten me what is missing?
from search page user can click this link:
<td><a href="getprofile.php?'.$field2name.'" target = "_blank">'.$field2name.'</td>
getprofile.php:
<?php
$conn = mysqli_connect("localhost","root","password","demo");
$pkey = mysql_real_escape_string($_GET[$field2name]);
// $pkey = "4027500001"; <-------if i put static data it can show profile data
$query = "SELECT * FROM nhc WHERE code =" . $pkey;
$result = $conn->query($query);
if ($result->num_rows > 0) {
echo "<table border='1'><tr><th>code</th><th>Name</th><th>class</th><th>AM</th><th>May GA</th><th>June GA</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["code"]."</td> <td>".$row["name"]."</td> <td> ".$row["class"]."</td> <td>".$row["am"]."</td> <td>".$row["may"]."</td><td>".$row["june"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
You are not assigning $field2name in any variable. Moreover your quotes are mismatching and are not forming a well formatted url
<td><a href="getprofile.php?value=<?=$field2name?>" target = "_blank">'<?=$field2name?></td>
now to get the value
use $_GET['value']
You need to give the GET parameter a name so that you can access it in PHP:
<td><a href="getprofile.php?code='.$field2name.'" target = "_blank">'.$field2name.'</td>
then in PHP:
$pkey = mysql_real_escape_string($_GET['code']);
Note that you should use prepared statements to protect yourself from SQL injection; mysql_real_escape_string is not sufficient (see this Q&A). For example:
$query = "SELECT * FROM nhc WHERE code = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('i', $_GET['code']);
$stmt->execute();
$result = $stmt->get_result();

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);
?>

I am using Chrome and cant get image to show from mysql using php

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'] ).'"/>';

Displaying Image from image uploader

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.

cant able to get the blob stored image into image format(MySql AND php)?

I am trying to get the image which is stored in the blob format in Mysql Database.I am using the following code.
index.php
<img src='image.php?questionid=questionid&question=question1&answer=answer1&author=myname' />
image.php
<?php
require_once('dbconfiguration.php');
$sql="SELECT image FROM tablename WHERE questionid='{$_GET['questionid']}' and question='{$_GET['question']}' and answer='{$_GET['answer']}' and author='{$_GET['author']}'";
$STH = $DBH->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
$row = $STH->fetch();
ob_clean();
header("content-type: image/jpg") ;
echo $row['image'] ;
?>
In index.php I didn't get any image.So When I enter the URL for image.php I am getting the following error in forefox.
The image “URL” cannot be displayed because it contains errors.
I am using php5.3 and mysql 5.1.36 .
What I did wrong.I went through almost all forums and no clues.Please help me on this.
It should be:
<?php
require_once('dbconfiguration.php');
$sql="SELECT image FROM tablename WHERE questionid='{$_GET['questionid']}' and question='{$_GET['question']}' and answer='{$_GET['answer']}' and author='{$_GET['author']}'";
$STH = $DBH->query($sql);
$STH->setFetchMode(PDO::FETCH_ASSOC);
$row = $STH->fetch();
ob_clean();
header("content-type: image/jpg") ;
echo $row['image'] ;
?>
As your database field is called image not Efaq_image

Categories