I can't display images from database MySQL - php

I can't display images from database.
I have folder called images next to index.php where I keep images.
I suppose I have wrongly defined folder path with images or sth else.
<?php
$connection = new mysqli("localhost", "root", "", "crud");
$sql = "SELECT * FROM test";
$res = $connection->query($sql);
if(#$res->num_rows > 0)
{
while($row = $res->fetch_assoc())
{
?>
<img src="<?php "C:/xampp/htdocs/img_mysql/images/".$row['image'] ?>" style="width:170px;height:120px" />
<?php
}
}
?>
I put picture with how look my website when I try display images.

2 Issues:
You are not outputing anything.
You are using a path to the file on your computer rather than of the
server itself.
Fix it like this:
<img src="<?php echo "/images/".$row['image'] ?>" style="width:170px;height:120px" />
You shouldn't use a domain in your frontend pathes for the simple reason that in production you will have a different domain. If you really want to for some reason, a fine solution will be using a constant, something like this:
[In a file included from all pages]:
define("IMAGES_PATH", "http://localhost/images/");
Then
<img src="<?php echo IMAGES_PATH . $row['image'] ?>" style="width:170px;height:120px" />

Related

How do I display an array of image from one row in my database? [duplicate]

i have tried to display an image from the php database and every time i load this code it takes me to a new page and it give me "image not able to be displayed" thing
<?php
while($data= mysql_fetch_assoc($query)){
?>
<img src=<?php
$image=$data['image'];
header("content-type: image/jpeg");
echo $image;
?>
/>
<?php } ?>
You have to create a php file, for example image.php. Inside it you can put something like that:
<?php
//Your code to call database for image id (from _GET['id']), for instance:
$image_id = $_GET['id'];
$data = mysql_query("Select image from images where id = $image_id");
while($data = mysql_fetch_assoc($query)){
$image=$data['image'];
header("content-type: image/jpeg");
echo $image;
}
?>
Then, in your view, you can do:
<img src="image.php?id=IMAGE_ID" />
Greetings!
send it to a to a different page on php and call it like this
<img src="differentpage.php?id=<?php echo id ?>">

Pulling an image from directory based on file name?

I'm trying to pull an image from a directory based on the file name matching the id number given in the URL. (ie: php?id=1 being the same as 1.png)
I've tried several different methods, and while I'm not getting any errors, I'm still getting no image that shows up when I type in the id tag.
Here's the most recent version of the code I've been working with:
<?php
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$img = $id;
foreach(glob("gallery3/var/albums/" . $img) as $filename)
?>
<img src = " <?php echo $filename; ?> " />
I'm at a loss. I've tried everything I can find and nothing seems to be working. I really don't want to pull the images from the database, and would prefer to pull them straight from the directory so that future uploaded images will be accessible directly from their id tag.
I think you're over-complicating it. All you need is this
<?php
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$img = $id;
?>
<img src="gallery3/var/albums/<?php echo $img; ?>.png" />
You could also go for:
<?php
if(isset($_GET['id']) && !empty($_GET['id']) && is_numeric($_GET['id'])) {
echo "<img src='gallery3/var/albums/".$_GET['id'].".png'/>";
}
else {
echo "Not Valid!";
}
?>
In the else statement at the bottom you could implement some form of error checking to present to the user in the event that a non valid input is received.

Avatar displayed Wordpress

I am trying to display an image on my wordpress-theme depending on which author wrote the page.
Therefore I am doing this:
<img src="<?php $autor=the_author_meta('display_name');
if (strpos($autor,'foo') !== false)
{echo esc_url(get_template_directory_uri() ); ?>/bilder/foo.jpg" />
<?php } else {echo esc_url( get_template_directory_uri() ); }
?>/bilder/fun.jpg" />
But this is throwing several errors. It's a) not pointing to the right path by not using get_template_directory_uri() at all. And it's b) adding me the Authorname within the url now like this
http://domain.com/Author%Name/bilder/fun.jpg
What am I doing wrong? And I don't want to use a plugin therefore - but thanks ;)
the_author_meta() is set to output the result, you want to just return it. So instead change it to get_the_author_meta().
Also I've tidied up your code, as I think there were some errors in it.
<img src="<?php $autor = get_the_author_meta('display_name');
if (strpos($autor,'foo') !== false) {
echo esc_url(get_template_directory_uri())."/bilder/foo.jpg";
} else {
echo esc_url(get_template_directory_uri())."/bilder/fun.jpg";
} ?>" />
When I replace foo with my user name, the image path is displayed as /bilder/foo.jpg, when I change it back to anything that isn't my username, it's shown as /bilder/fun.jpg, so I believe it works fine. The template URL appears fine too.
<img src="http://test.local/wp-content/themes/test/bilder/fun.jpg">

echo images using php

what am i trying to do is echo an image using php,
my code is really simple..
i have stored the path of the image in mysql db..
the path of the image is: ../users/profiles/23/images/dps/1409947526.jpg
now i am using the following code to output this picture:
mysql_connect("localhost", "root", "") or die("error!");
mysql_select_db("xone");
$query = mysql_query("SELECT * FROM userdpcover WHERE id='23'");
$result = mysql_fetch_array($query);
$dir = $result['dp_address'];
$dp_name = $result['dp_name'];
$dp = $dir.$dp_name;
echo $dp;
echo "<img src='$dp' />";
but when i run this code, all i get is an broken image!
thanks in advance!
Have you tried this?
echo "<img src='".$dp."' />";
Please try this and please check your image path url correct....
<?php
//here your code
?>//close php tag and try this ...
<img src="<?php echo $dp; ?>"/>
<?php
//your code here
?>
View the source of the output HTML. This will tell you the image path. You probably need to modify it.
Also, remember to set your base URL in a PHP config file to avoid repeating URL paths.

display image using php

I have a the following code that i intend to should return a filepath name from mysql in the php section and then display the image in html. But i am only getting up a tiny thumbnail in my browser. By the way "username" and "imagefile" are the only columns in the table "images".
I'm sure there is just some silly mistake but i need a fresh pair of eyes to spot it. Thanks a lot in advance. P.S i know i should really me moving over to mysqli but i will simply translate at a later date. Cheers
<?php
session_start();
$username = $_SESSION['username'];
$con = mysql_connect('localhost','root','password');
mysql_select_db("db");
$profileimage = mysql_query("
SELECT * FROM images WHERE username='$username'
");
$row = mysql_fetch_array($profileimage);
$showimage = $row['imagefile'];
?>
<html>
<img src = "$showimage">
</html>
First off, HTML doesn't know what "$showimage" means. That is a PHP variable and HTML cannot interpret it. You need to output it so that HTML can just deal with the result.
So if the value for $showimage is "/images/foo.jpg" you would need something like:
<img src="<?php echo $showimage; ?>" />
which would give you
<img src="/images/foo.jpg" />
Now, switching things to mysqli is as simple as replacing mysql with mysqli. It's no more complicated than that. Since it looks like you are just starting to learn about these things you may as well, when you go to improve things, learn about PDO.
Is this your current real code or is it a simplified version? If it is your real code the problem is in the HTML part where the PHP variable is unknown, you should do this:
<html>
<img src ="<?php echo $showimage; ?>" />
</html>
<?php
$db = mysqli_connect("localhost:3306","root","","databasename");
$sql = "SELECT * FROM table_name ";
$sth = $db->query($sql);
while($result=mysqli_fetch_array($sth)){
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'" height="100" width="100"/>';
}
?>
Works for me

Categories