How to show image (name) in DB using php, - php

I am facing a problem in showing images in my page using PHP. I have a table "images" in my db. I have saved the names of the images in this table which i am retrieving using PHP. My problem is that the images are not being shown due to unknown logical error. I have given both the absolute and the relative paths of the images in the tag but all in vain. Please suggest me what wrong i am doing in retrieving the code.
My PHP:-
<?php
require_once("includes/database.php");
$img_no = $_GET["img_no"];
$query = "SELECT * FROM `images` WHERE `img_no`=".$img_no;
$result = mysqli_query($con, $query);
?>
HTML:-
<div class="project owl-carousel">
<?php while($row = mysqli_fetch_array($result)){ ?>
<div class="item">
<?php echo "<img class='img-responsive' src='img/".$row['img']."png'> ";?>
</div>
<?php }; ?>
</div>
In the img src, I have given both the absolute and the relative paths of the images folder, but did not work!
Please suggest!
Thanks in advance!
Regards!

Does your image extention(.png) is same to what you have written in your code.
if yes then check whethere you have saved your image name in database with extention or without it.

Related

How to display images using php when images in folder / DB

I have a DB on my localhost with a table that contains images that belong to various categories (image_category). The images are contained in a folder i.e.: /gallery/xxxIMAGESxxx one step higher from where I am keeping the php file with the hmtl code inside for display on a users screen.
Right now the only problem I face is that when I want to display the images on my website, they do not display. Instead I only get the Title of the image and the description which also comes from the DB.
In the style section of my anchor tag i included
background-image: url(gallery/'.$row["imgFullNameGallery"].')">
where the image should display.
I think this has to do with Base64 encoding but I cannot point out where and how I should code this / set the syntax. If this is the problem.
Help would be much appreciated. Thx in advance.
//this is the php code with the call to the table with the images
<div class="display_CNC_Machining_images">
<?php
include_once 'includes/dbh.inc.php';
$sql = "SELECT * FROM gallery WHERE image_category = 'CNC_Machinery'";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt,$sql)) {
echo 'SQL statement failed!';
} else {
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result)) {
//what's echoed out by the database
echo ' <a class="images" style="background-repeat:no-repeat; background-image: url(gallery/'.$row["imgFullNameGallery"].')">
<div class="color-overlay">
<h3>'.$row["titleGallery"].'</h3>
<p>'.$row["descGallery"].'</p>
</div>
</a>';
}
}
?>
</div>
Expected result:
Image from DB / localfolder is displayed together with image title and image description from DB.
Actual result:
Only image title and image description from DB is displayed.
Database with images
displayed on my screen at the moment, no imgs
Instead of doing this:
echo '<a class="images" style="background-repeat:no-repeat; background-image: url(gallery/'.$row["imgFullNameGallery"].')">
<div class="color-overlay">
<h3>'.$row["titleGallery"].'</h3>
<p>'.$row["descGallery"].'</p>
</div>
</a>';
Do this:
$image = $row["imgFullNameGallery"];
echo '<a class="images" style="background-repeat:no-repeat; background-image: url(gallery/'.$image.')">
<div class="color-overlay">
<h3>'.$row["titleGallery"].'</h3>
<p>'.$row["descGallery"].'</p>
</div>
</a>';
Placing your $row["imgFullNameGallery"] into an apart variable will prevent conflicts.
The double quotes will get in conflict with the quotes from your style.
This should show the image.
Also check if your image exists in your folder.

Images from mysql does not display using php

Image is successfully fetching from database but it not be shown by the PHP. In place of image it shows a iamge thumbnail.
If I use header('Content-type:image/jpg'); it will show the only thumbnail all page contents disappear.
include 'functions/connect.php';
$user = $_SESSION['email'];
$sql = "SELECT photo FROM user WHERE email='$user'";
$run_sql = mysqli_query($conn,$sql);
$row = mysqli_fetch_array($run_sql);
$user_photo =$row['photo'];
echo"<p><img src='$user_photo'></p>";`
You can try something like this:
<img alt="<?php echo $user_photo; ?>" src="uploads/<?php echo $user_photo; ?>" /> // Upload is folder where image was uploaded
It depends on what the field "photo" contains. If the photo field contains the location of the picture on your system, then the way you have done it should work. However, if the field contains the actual photo in binary format (having field data type as 'blob') then you'll need to do the following:
file_put_contents("image.jpg", $user_photo);
echo "<p><img src='image.jpg' ></p>";

Display lightbox image retrieved from longblob database

I'm using the lightbox plugin: Fancybox
I wanted to apply lightbox effect on image gallery php file. The image is stored in longblob format instead of folder. Following is the code I did to read image.
<?php
$img_slct=mysql_query("SELECT * FROM news_gallery WHERE news_id='$page[news_id]' ORDER BY position ASC");
while($img=mysql_fetch_array($img_slct))
{ ?>
<img src="load_image.php?id=<?php echo $img['file_id']; ?>"/>
<?php } ?>
load_image.php
$id=$_GET['id'];
$is_file=false;
if(!empty($id))
{
$file=mysql_query("SELECT id FROM news_files WHERE id='$id'");
if(mysql_num_rows($file)>0)
{
$is_file=true;
$file=mysql_fetch_array($file);
}
}
if($is_file)
display_file_news($id);
Everything is displayed correctly but that is an issue when using lightbox to display the image retrieved from longblob database.
<a rel="example_group" href="load_image.php?id=<?php echo $img['file_id']; ?>">
<img src="load_image.php?id=<?php echo $img['file_id']; ?>"/>
</a>
I have no idea how to read the longblob database using lightbox. So I simply put like this --> href="load_image.php?id=<?php echo $img['file_id']; ?>"
I know that is wrong, but can you help me? Please...Let me know if you need more info, because I can't insert all my code at here. Thank you.
Following are my database tables.
news_files table
news_gallery table

how to display image on web page from mysql in php

I have to display images on my php web page from mysql database where i have give the path of my images. but Its only displaying the path but not the image.
I used the following Code:
<?php
$servername="localhost";
$username="root";
$conn= mysql_connect($servername,$username)or die(mysql_error());
mysql_select_db("ek",$conn);//EK is my Database
$sql="select Pics from images";
$result=mysql_query($sql,$conn) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$image = $row['Pics'];
print $image;
?>
and the output I am getting is :
images/DSC01750.jpg
Some body can you please help me to actually displaying the image but not the path.
Many Thanks
If you wish to display an image in an HTML page, you can construct the image tag like this:
<img src="<?php echo $image; ?>" />
If you are attempting to obscure the location of an image and serve it via the PHP script, your image tag will look something like this:
<img src="yourscript.php" />
and you can modify your script to open and output the file:
$image = $row['Pics'];
header("Content-Type: image/jpg");
readfile( $image );
If $row['Pics'] contains the path to the image, then you'll need to translate that into an image reference to direct the browser to that path. Something like this:
<img src="<?php echo $image; ?>" />
Note that "path" can mean something entirely different here. If it's a server-side fully qualified path, you'll want to translate it into an application-relative path usable by the browser. Something that begins with /usr/web/something/blah/ won't be useful to the browser.
You need to wrap it in an img tag.
echo "<img src=\"$image\">";
To display several images
<?php
$servername="localhost";
$username="root";
$conn= mysql_connect($servername,$username)or die(mysql_error());
mysql_select_db("ek",$conn);//EK is my Database
$sql="select Pics from images";
$result=mysql_query($sql,$conn) or die(mysql_error());
$i=0;
$display_num =4;
while($row = mysql_fetch_assoc($result) && $i++ < $display_num){
$image = $row['Pics'];
echo "<img src=\"$image\">";
}
?>

Automatically create php pages

I have an issue on how to create individual php pages automatically.
I have already created a page called catalog.php.
In catalog.php, mySQL query would take place, querying:
$link = mysql_connect("localhost", "root", "");
mysql_select_db("photodb", $link);
$sql = "SELECT id, title, caption, comments, imagelink, year FROM photo";
Then this query would loop and display the contents on catalog.php side by side:
<ul class="grid">
<?php while ($row = mysql_fetch_assoc($result))
{ ?>
<li>
<p class="image"><img src="getImage.php?id=<?php echo $row['id']; ?>" alt="" width="175" height="200" /></p>
<p class="name"><?php echo $row['title']; ?></p>
<p class="year"><?php echo $row['year']; ?></p>
</li>
<?php } // while
?>
</ul>
where class="grid" would arrange all the queried data side by side, with image, title and year being displayed.
However, one requirement that i need is that whenever i click on any of these images, it should link to its own php page(individual.php) to show a detailed image, title, caption and author's comments. An elaboration is shown below:
Title
IMAGE Caption
Author's comments
An example:
Picture of bridge
IMAGE This image was taken in paris
Low Shuttle Speed
In the above, IMAGE, caption, title, author's comments are found in the same database "photodb" in the same table "photo"
My issue here is as follows:
Is there a way to create such pages automatically? The reason being is, if I create them manually I would have a hard time because my database has more that 100 entries.
In the <a href=""> tag as seen in catalog.php, what should the values be?
I already have a template for such a "individual.php" page. An example of he structure is follows:
<body>
<p class="title">
London Bridge</p>
<p class="caption">
Image taken in summer 2009<br /><br /></p>
<p class="image">
<img border="0" class="floatleft" src="imagelink" width="250" height="400" />
Low Shuttle Speed
</p>
</body>
How can i change this structure to suit my requirements?
Four. Can such "individual.php" url be renamed to something else with a unique id? Each image found in the database has their own unique ID.
Five. I have already queried the database in catalog.php. Can i somehow reuse this query individual.php?
In case you are wondering, getimage.php is as follows:
<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe
$link = mysql_connect("localhost", "root", "");
mysql_select_db("photodb", $link);
$sql = "SELECT imagelink FROM photo WHERE id=$id";
$result = mysql_query($sql, $link);
$row = mysql_fetch_assoc($result);
mysql_close($link);
header("Content-type: image/jpeg");
echo file_get_contents($row['imagelink']);
?>
Many thanks for reading my questions.
Your last code snippet essentially answered your question - you'll create another page and pass a variable to it through the URL ($_GET). So your URL may look like "individual.php?id=25" which would pull the image with the ID of '25'. $_GET['id'] would be '25' in this example.
I also noticed you are using the "root" user for the MySQL connection. I would strongly advise against that.
http://www.1stwebdesigner.com/tutorials/getting-started-php-dynamic-content/
http://www.greensql.com/articles/mysql-security-best-practices
With reference to #Nathan Loding. I would also use
mysql_real_escape_string()
Obviously to prevent mySQL attacks. Judging by some of your code above, I guess your new or an "OLDISH" weby copy and paste coder??
These functions will help and help you learn too.. very easy to do this... so..yes ..You can use file functions.
fopen()
See also Appendix M, fclose(), fgets(), fread(), fwrite(), fsockopen(), file(), file_exists(), is_readable(), stream_set_timeout(), popen(), and stream_context_create().

Categories