I have a working code that pulls image blobs from the MYSQL Table and shows them on my web page. It works for the most part but the problem I am running into is how to not make it move to the next line as shown in my picture below. Is there anyway to do this?
<?php
$id ='1';
$db = mysqli_connect("localhost","brianrob_usr","","brianrob_productdb"); //keep your db name
$sql = "SELECT * FROM Products WHERE id = $id";
$sth = $db->query($sql);
while($row = $sth->fetch_array()){
echo '<div><img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/></div>';
}
?>
#charset "utf-8";
/* CSS Document */
body{
width: inherit;
height:inherit;
background-image: url("007-dark-loom.png");
}
as Given by ADyson, Removing the tags helped fix this issue.
<?php
$id ='1';
$db = mysqli_connect("localhost","brianrob_usr","Ilikecandy2009","brianrob_productdb"); //keep your db name
$sql = "SELECT * FROM Products WHERE id = $id";
$sth = $db->query($sql);
while($row = $sth->fetch_array()){
echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['Image'] ).'"/>';
}
?>
Related
I want to display all the user account photo and name from database and put one user photo and name in a div tag and similarly with all the accounts with a link that redirect to the clicked account. But I am having problem on how to display it.If its still not clear try picturing an online shopping site where each product with its name is in its own box separately. Please help, I am actually trying to make a social networking site for my educational project. Thank You.
Heres my code:
$sql = "SELECT * FROM user WHERE NOT username = '$username'";
$account = array();
$photo1 = array();
$result = $conn->query($sql);
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
$account[] = $row['username'];
$a = $row['photo'];
$photo1[] = "<img src = '$a' alt = 'profile photo' width = '100px' height = '100px'>";
}
}
and Heres a section of my html code:
<div>
<?php echo implode(', ',$photo1); ?>
<?php echo implode(', ',$account);?>
</div>
This is the image that I uploaded
Could you add some wireframe for your task?
In this way i think you need something like this:
<div>
<img src="$imgSource" alt="" title="">
$userName
</div>
#try this and then add html as you need
$sql = "SELECT * FROM user ";
$result = $conn->query($sql);
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo $row['username'];
$a = 'www.yourdomain.com/usriamegefolder/'.$row['photo'];
echo "<img src = '$a' alt = 'profile photo' width = '100px' height = '100px'>";
}
}
I want to retrieve an image on my database,and I also want to retrieve images even it is null. now what i have on my image folder is an avatar, the avatar is also retrieve when the field is null. and when it is not null the image on the database will retrieve how can I do it ? how can I do it on this statement? thanks
here is my database connection
<?php
session_start();
error_reporting(0);
include('includes/config.php');
$result = mysqli_query($db, "SELECT * FROM shopusers");
$id= $_SESSION['id'];
$query = "SELECT * FROM shopusers WHERE id=$id";
$results = mysqli_query($con, $query);
$num=mysqli_fetch_assoc($results);
?>
here is my fetch query
<?php
$query=mysqli_query($con,"select * from shopusers where id='".$_SESSION['id']."'");
while($row=mysqli_fetch_array($query))
{
?>
<?php
if($row == null){
echo "<div class='avatar'><img src='img/avatar-6.jpg' alt='...' class='img-fluid rounded-circle'></div>";
}else{
echo "<div class='avatar'><img src='users_image/".$row['image']."' alt='...' class='img-fluid rounded-circle'></div>";
}
?>
<?php } ?>
Storing images in database is not a good idea because your application will require more memory.
Since OP mentioned it's a must to store in database, you can output it via data src.
<?php
if(empty($row['image'])){
echo "<div class='avatar'><img src='img/avatar-6.jpg' alt='...' class='img-fluid rounded-circle'></div>";
}else{
echo '<div class="avatar"><img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"' alt="..." class="img-fluid rounded-circle"></div>';
}
?>
<?php } ?>
I have a page which view links from the database.
Now I want that if i pressed on any of these links i will be in another page but still have the variable name i have presses to do some queries on it.
here is the code of the first page:
$query = "SELECT Category FROM product WHERE Visible = 0";
$res = mysqli_query($con,$query);
echo "<div style = 'font:40px/60px Arial; color :orange'>All Categories Available :-</div>";
while($row = mysqli_fetch_array($res))
{
$c = $row['Category'];
echo '<li>'.''.$c.''.'</li>';
}
Try :
in your first page:
$query = "SELECT Category FROM product WHERE Visible = 0";
$res = mysqli_query($con,$query);
echo "<div style = 'font:40px/60px Arial; color :orange'>All Categories Available :-</div>";
while($row = mysqli_fetch_array($res))
{
$c = $row['Category'];
echo '<li>'.'<a href ="page2.php?value=>'.$c.'</a>'.'</li>'; // i put value as variable in redirected page.
}
in your second page like page2.php :
if(isset($_GET['value'])){
$url_val = $_GET['value'];
echo $url_val; // your first page value.
// do your work
}
This will helpful for you.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How do i display an image stored in mySQL database as BLOB ?
What it tried so far:
1. Created a new php function/file to get picture (getpicture.php).
2. In the html, I have the following code:
<img src="getpicture.php?id=2" border ="0" height="250" width="250" />
/*below is the getpicture.php*/
<?php
# $db = new MySQLi('localhost','root','','myDatabase');
if(mysqli_connect_errno()) {
echo 'Connection to database failed:'.mysqli_connect_error();
exit();
}
if(isset($_GET['People_Id'])) {
$id = mysqli_real_escape_string($_GET['People_Id']);
$query = mysqli_query("SELECT * FROM 'people' WHERE 'People_Id' = '$id'");
while($row = mysqli_fetch_assoc($query)) {
$imageData =$row['image'];
}
header("content-type: image/jpeg");
echo $imageData;
echo $id;
}
else {
echo "Error!";
echo $id;
}
?>
What's wrong with the codes ? Please help!
I answered my own question, it's working now..
Below is the getpicture.php:
<?php
$db = new MySQLi('localhost', '', '', 'mydatabase');
if ($db->connect_errno) {
echo 'Connection to database failed: '. $db->connect_error;
exit();
}
if (isset($_GET['id'])) {
$id = $db->real_escape_string($_GET['id']);
$query = "SELECT `Picture` FROM member WHERE `Id` = '$id'";
$result = $db->query($query);
while($row = mysqli_fetch_array($result)) {
$imageData = $row['Picture'];
header("Content-type:image/jpeg");
echo $imageData;
}
}
?>
The php script which retrieve the getpicture.php above looks like this:
echo '<img src="getpicture.php?id=' . htmlspecialchars($_GET["id"]) . '"border ="0" height="250" width="250" />';
Thaank you all for the help
This is wrong:
$query = mysqli_query("SELECT * FROM 'people' WHERE 'People_Id' = '$id'");
you use wrong quotes for table name (must be backtick instead of single quote (see tylda ~ key). See docs: http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
Also this is wrong too
header("content-type: image/jpeg");
echo $imageData;
echo $id;
get rid of last echo $i; and replace it with exit(); otherwise you corrupt the image data stream you just sent.
Plenty is wrong.
your SQL is wrong. Remove the single quotes from the table and column and replace with back ticks.
Although it may still work, you should have a couple of new lines after your header
You shouldn't echo out your $id after you echo your image data.
You're checking for the wrong value when you check isset
Also, you should be using OOP for mysqli
Since your image data is only a single row, you don't need to wrap it in a while loop
Here is an updated code example
<?php
$db = new MySQLi('localhost', 'user', 'password', 'myDatabase');
if ($db->connect_errno) {
echo 'Connection to database failed: '. $db->connect_error;
exit();
}
if (isset($_GET['id'])) {
$id = $db->real_escape_string($_GET['id']);
$result = $db->query("SELECT * FROM `people` WHERE `People_Id` = '$id'");
$row = $result->fetch_assoc();
$imageData = $row['image'];
header("Content-type: image/jpeg\n\n");
echo $imageData;
}
else {
echo "Error!";
}
This question already has answers here:
How to retrieve images from MySQL database and display in an html tag
(4 answers)
Closed 1 year ago.
I am trying to display an image coming from the database and I was not able to display the image .but its showing like this user-1.jpg Please see my code can one guide me how to display the image.
$sqlimage = "SELECT image FROM userdetail where `id` = $id1";
$imageresult1 = mysql_query($sqlimage);
while($rows = mysql_fetch_assoc($imageresult1))
{
$image = $rows['image'];
print $image;
}
Displaying an image from MySql Db.
$db = mysqli_connect("localhost","root","","DbName");
$sql = "SELECT * FROM products WHERE id = $id";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'"/>';
For example if you use this code , you can load image from db (mysql) and display it in php5 ;)
<?php
$con =mysql_connect("localhost", "root" , "");
$sdb= mysql_select_db("my_database",$con);
$sql = "SELECT * FROM `news` WHERE 1";
$mq = mysql_query($sql) or die ("not working query");
$row = mysql_fetch_array($mq) or die("line 44 not working");
$s=$row['photo'];
echo $row['photo'];
echo '<img src="'.$s.'" alt="HTML5 Icon" style="width:128px;height:128px">';
?>
<?php
$connection =mysql_connect("localhost", "root" , "");
$sqlimage = "SELECT * FROM userdetail where `id` = '".$id1."'";
$imageresult1 = mysql_query($sqlimage,$connection);
while($rows = mysql_fetch_assoc($imageresult1))
{
echo'<img height="300" width="300" src="data:image;base64,'.$rows['image'].'">';
}
?>
You need to do this to display image
$sqlimage = "SELECT image FROM userdetail where `id` = $id1";
$imageresult1 = mysql_query($sqlimage);
while($rows=mysql_fetch_assoc($imageresult1))
{
$image = $rows['image'];
echo "<img src='$image' >";
echo "<br>";
}
You need to use html img tag.
put you $image in img tag of html
try this
echo '<img src="your_path_to_image/'.$image.'" />';
instead of
print $image;
your_path_to_image would be absolute path of your image folder like eg: /home/son/public_html/images/ or as your folder structure on server.
Update 2 :
if your image is resides in the same folder where this page file is exists
you can user this
echo '<img src="'.$image.'" />';
instead of print $image; you should go for print "<img src=<?$image;?>>"
and note that $image should contain the path of your image.
So,
If you are only storing the name of your image in database then instead of that you have to store the full path of your image in the database like /root/user/Documents/image.jpeg.
Simply replace
print $image;
with
echo '<img src=".$image." >';
$sqlimage = "SELECT image FROM userdetail where `id` = $id1";
$imageresult1 = mysqli_query($link, $sqlimage);
while($rows=mysqli_fetch_assoc($imageresult1))
{
echo "<img src = 'Image/".$row['image'].'" />';
}
<?php
$conn = mysql_connect ("localhost:3306","root","");
$db = mysql_select_db ("database_name", $conn);
if(!$db) {
echo mysql_error();
}
$q = "SELECT image FROM table_name where id=4";
$r = mysql_query ("$q",$conn);
if($r) {
while($row = mysql_fetch_array($r)) {
header ("Content-type: image/jpeg");
echo $row ["image"];
}
}else{
echo mysql_error();
}
?>
sometimes problem may occures because of port number of mysql server is incoreect to avoid it just write port number with host name like this "localhost:3306"
in case if you have installed two mysql servers on same system then write port according to that
in order to display any data from database please make sure following steps
1.proper connection with sql
2.select database
3.write query
4.write correct table name inside the query
5.and last is traverse through data
put this code to your php page.
$sql = "SELECT * FROM userdetail";
$result = mysqli_query("connection ", $sql);
while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
echo "<img src='images/".$row['image']."'>";
echo "<p>".$row['text']. "</p>";
}
i hope this is work.