Getting information from image using mysql database - php

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.

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

Getting an image from a database

I have a getimage.php containing
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "user", "password");
mysql_select_db("database");
$sql = "SELECT photo FROM property_photo WHERE id=$id";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo $row['property_photo'];
?>
and my index.php
<div class="bloco"> <img src="getImage.php?id=1" ?>
<div>Description</div>
</div>
<div class="insert"></div>
</div>
i can't see the photo, It won't display or it displays as a blank box with a blue question mark.
In your query you are selecting the field 'photo' but when you come to echo the field you actually echo the name of the table:
echo $row['property_photo'];
So I believe your actually getting a hidden fatal error instead of the image saying - unknown index 'property_photo'
try
echo $row['photo'];
instead.
use ob_clean() before header,
it may solves your problem

How to grab the id of the search result came from

What you are seeing is my code that displays images as a search result. I have an anchor down there so when you click on the picture it sends you to a TEST page.
I want to have a page set up that will display the rest of the row entries that are associated with that picture:
(Picture) Player: Steve Sax
Card Type: Donruss
Year: 1989
Value: $2.00
How do I grab the "id" in the row of the search result and then echo it in a table that shows up on the TEST page?
<?php
$servername = "*********";
$username = "*********";
$password = "*********";
$dbname = "*********";
$username1=$_SESSION['activeusername'];
$userid = $_SESSION['activeid'];
$userid = $_SESSION['activeid'];
$itemid = $_SESSION['activeid'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM useritems JOIN (users, iteminfo) on (users.id=useritems.UserID AND iteminfo.ID=useritems.ItemID) AND userid='2'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<a href='test1.php'><div id='frame'>
<div id='splash-image' style='background: url(".$row['imagename']."); background-size:cover;'></div>
<div id='text'>
<table >
<tr></tr>
</table>
</div>
</div>";
}
} else {
echo "You Have No Items In Your Closet!!!";
}
mysqli_close($conn);
?>
Pass the id with get as:
echo "<a href='test1.php?id=". $row['id']."'>
And get it on the landing page as:
$id=$_GET['id'];
echo $id;
Assuming the id you want is the item's id, you would echo out
$row['ItemID']
If you wanted to include this ID into your href as a GET parameter you could do something like:
echo "<a href='test1.php?ItemID=" . $row['ItemID'] ."'>...</a>";
Then, in your test1.php, you can retrieve the Item ID for use in your query (after sanitizing it!) by accessing the $_GET global.
$ItemID = $_GET['ItemID'];
If you just wanted the result set row number (not tied to user id or item id), you could echo out something like:
$counter++
or take a look at MySQL - Get row number on select

get one variable out of database with PHP

Ok obviously i'm a beginner.
What i'm trying to do here is probably simple.
I'm trying to get all the field of my database to echo on my website. So i use this to show only the basic details:
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases")
or die(mysql_error());
while($row = mysql_fetch_array( $result )
) {
// Print out the contents of each row into a table
echo '<ul class="releaselist">';
echo '<li>';
echo $row['products_name'];
echo '</li>';
echo '<li>';
echo $row['products_title'];
echo '</li>';
echo '<li>';
echo '<img class="releaseimg" src="'.$row['products_image'].'">';
echo '</li>';
echo '</ul>';
}
?>
So far eveything works fine.
What i want to do with this is get my URL to look like this when i click on a single image.
www.mywebsite.com/detailed.php?id=1
and show everything available in the table RELEASES
The way i've done it which is not working is:
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases
WHERE products_id=products_id")
or die(mysql_error());
if ($result){
$row = mysql_fetch_array($result);
echo $_GET['products_id'];
}
?>
If I'm understanding you correctly, your first script needs to be updated to:
echo '';
That will turn your image into a link. At that point your second script should be able to use:
_GET['id']
to retrieve this value and use it in your query. Thusly:
$result = mysql_query("SELECT * FROM releases
WHERE products_id=" . mysql_real_escape_string($_GET['id']));
Now if you really want to products_id in the URL instead of id, just change it 'id' to products_id in the echo line in the first script and all of the $_GET uses in the second script.
Try changing this line:
echo '<img class="releaseimg" src="'.$row['products_image'].'">';
To this one:
echo "<a href='detailed.php?products_id=".$row['products_id']."'><img class='releaseimg' src='".$row['products_image']."'></a>";
Hmm try using $row['products_id'] instead of $_GET['products_id']; $_GET is used when accessing a webpage and the reason that $row['products_image'] worked is because you accessed the database results.
You have misplaced the use of products_id...
Your code should like something like this...
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("crnew") or die(mysql_error());
$result = mysql_query("SELECT * FROM releases
WHERE products_id='".intval($_GET['id'])."'")
or die(mysql_error());
if ($result){
$row = mysql_fetch_array($result);
echo $row['products_id'];
}
?>

Categories