I'm trying to display an image from a MySQL database and my code works fine if the image is a JPG, but if I modify the content-type to display a PNG, it doesn't work...
How do I make it work for PNG??
<?php
// configuration
require("../includes/config.php");
header('Content-type: image/JPG');
$username = "xxxxx";
$password = "*****";
$host = "localhost";
$database = "database";
#mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());
#mysql_select_db($database) or die("Can not select the database: ".mysql_error());
$query = mysql_query("SELECT * FROM images");
while($row = mysql_fetch_array($query))
{
echo $row['image'];
}
?>
Also, is it possible to display the image along with its name and description?
Thanks!
If you don't mind not supporting older browsers, you can use data urls to display your images and a description http://en.wikipedia.org/wiki/Data_URI_scheme.
<figure>
<img src="data:image/png;base64,<?php echo base64_encode($row['image']); ?>" alt="Your alternative text" />
<figcaption>Some more description</figcaption>
</figure>
Saving images in a database is not very useful in almost all cases. You should be carefull with upper and lower letters in the mimetype, see http://de.selfhtml.org/diverses/mimetypen.htm (it's german, but you will be able to read the list). And as a last advice - look at the mysqli_* functions http://www.php.net/manual/en/book.mysqli.php.
// Edit: Just an idea, but if you have multiple images in the database your image might be broken, because you just put them all into one image. This will not work! With your code, you can only display one image at once.
Related
I am having trouble with displaying images with php. I have tried this question, but it's still not displaying properly.
I have a PHP file that is handling the HTML format, the code for the image part is as follows:
while($row = mysql_fetch_array($result)) {
$partNo = $row['partNo'];
echo "<tr>";
echo '<td> <img src="getThumb.php?id =\''. $partNo .'\' width="50" height="50" /> </td>';
My getThumb.php is as follows:
<?php
$id = $_GET['id'];
$con = mysql_connect("localhost", "root", "root") or die("Unable to connect to server");
mysql_select_db("bmworld.mu");
$sql = "SELECT thumbnail FROM part WHERE partNo=$id" or die ("Could not fetch thumbnail");
$result = mysql_query("$sql");
$row = mysql_fetch_object($result);
mysql_close($con);
header("Content-type: image/jpeg");
echo $row['thumbnail'];
?>
The results is as follows:
http://postimg.org/image/xqcq16ucf/
My image is of type 'blob' saved in MySQL, and not a path to the image folder. I want to retrieve that blob from MySQL, and display it directly from the retrieved blob instead of a path to the image, just like the other columns of data from my database table. How can I do this?
There are following problems may be in your code:-
your image path is not correct (what you are saving in your database).
Either you are saving your image directly with blob data-type and at the time retrieval you face some problem.
For the first problem try to hardcode path of your image and check, if working then your path saving to databsae is not correct and you have to correct.
For second problem please check this link:PHP display image BLOB from MySQL. It will definitely help you. Thanks.
are u sure that getThumb.php has good file location?
<img src="getThumb.php?id =\''. $partNo .'\' width="50" height="50" />
The main idea is being able to controll, upload and delete a slideshow's images from a database.
So, first I connect to the database like this, and with a while function I echo all the images I have stored in my db.
This is the code to do that:
<?php
$servername = "myservername";
$username = "myusername";
$password = "mypassword";
$dbname = "mydbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT id, img FROM mytable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<img src=".$row['img'].">";
}
} else {
echo "0 results";
}
?>
In the table "mytable" I have 2 columns: an ID auto-inc and a column called img.
How can I store an image in the img column so that my function will echo all the images I have in my table?
All I found searching til now was a lot of methods that upload the image with an html form, but I'd prefer to upload my img directly on my database, without building an html+php form to upload all my images to my table.
Is that possible?
Storing entire images in a database is generally a bad idea. It is much simpler to simply store the path of the image inside your database, as has already been mentioned. You may have a bit of extra work deleting the images, as you would need a php system call to delete the image file.
If you really must store images inside a database, you can save them as base64 encoded text. I've seen this be done at a fairly successful company, but no particularly good reason was given for this. Your sql execution time will be substantially larger, because images are large files, and your table size will balloon.
I am just starting to use html, php, and mysql. I've successfully logged into my database using php, and formed a query. I now want to go a step further and show a picture instead of just strings or numbers.
The variable 'result' will be returning a string that has a url to an image i want to display on this webpage. How would I do this?
<html>
<head>
<title>My First Question</title>
</head>
<body>
<?php
$dbhost = 'someURL.com';
$dbname = 'user';
$dbuser = 'user';
$dbpass = 'password';
$mysql_handle = mysql_connect($dbhost, $dbuser, $dbpass)
or die("Error Connecting To Database Server");
mysql_select_db($dbname, $mysql_handle)
or die("Error selecting database: $dbname");
echo 'Successfully connected to database!';
$first = 'bobbie';
$query = sprintf("SELECT image FROM Player
p WHERE name='%s'", mysql_real_escape_string($first));
$result = mysql_query($query);
mysql_close($mysql_handle);
?>
</body>
</html>
Inside PHP, This will turn your SQL response into a usable variable.
$result = mysql_fetch_assoc(mysql_query($query));
Outside of your PHP tags, Echo the URL from the table into the SRC of an IMG element.
<img src="<?= $result['url_column_name'] ?>"/>
This will create a new IMG element with the source being the URL that you have fetched from your SQL query.
Short tags are also a way of echoing PHP variables in HTML.
<?= $var1, $var2 ?>
is the equivalent of using
<?php echo $var; echo $var2; ?>
This is a simple case of echoing the relevant HTML. You'll also have to fetch the results after you execute the query -
$result = mysql_query($query);
$data = mysql_fetch_assoc($result);
echo '<img src="'.$data['image'].'" />;
For added security, a good practice would be to escape any possible unwanted HTML content in your images path - htmlspecialchars($data['image']).
It should also be noted here that you are using a very old deprecated method to access your database. You might want to think about updating your code to use more modern PDO methods.
So what? simply use it as a source to your image
<?php $imgname = mysqli_fetch_array($connection, $result); ?>
<img src="<?php echo $imgname['image_column_name']; ?>" />
And btw use mysqli_() or PDO instead of using mysql_() as community is not maintaining it anymore
Once you update your mysql to mysqli you can echo the image's url in an html img tag as so:
echo '<img src="'.$result['image'].'"/>';
<?php
$dbhost = 'someURL.com';
$dbname = 'user';
$dbuser = 'user';
$dbpass = 'password';
$mysql_handle = mysql_connect($dbhost, $dbuser, $dbpass)
or die("Error Connecting To Database Server");
mysql_select_db($dbname, $mysql_handle)
or die("Error selecting database: $dbname");
$first = 'bobbie';
$query = sprintf("SELECT image FROM Player
p WHERE name='%s'", mysql_real_escape_string($first));
$result = mysql_query($query);
mysql_close($mysql_handle);
header("Location: $result");
?>
should work
Hello i am having a site which shows users their own profile picture. But i am unable to do so, I have built a code but it is not working & giving an error as Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/u522159750/public_html/users/myaccount.php on line 156 i.e. the line $sql = mysqli_query($link, "SELECT id, name, avatar FROM users WHERE id="'.$id.'" ") ;Can anyone suggest me what could be the possible reasons. The code for showing image is as follow---
<p>
<?php
//We check if the users ID is defined
$db_host = "xxxxxxxxxxxx";
$db_username = "xxxxxxxxxxxxxx";
$db_password = "xxxxxxxxx";
$db_database = "xxxxxxxxxxxxxx";
$link = mysqli_connect($db_host,$db_username,$db_password) or die("Cannot connect");
mysqli_select_db($link, $db_database) or die("Cannot select the database");
$sql = mysqli_query($link, "SELECT id, name, avatar FROM users WHERE id="'.$id.'" ") ;
while($result = mysqli_fetch_object($sql)):
<img src="<?php echo $result->avatar; ?>" alt="<?php echo $result->name; ?>" width="200" height="300" />
?> </p>
<?php endwhile; ?>
The part where you intend to output the link with the image, is html and should either be outside of the php part, or printed, as below.
<?php
//We check if the users ID is defined
$db_host = "xxxxxxxxxxxx";
$db_username = "xxxxxxxxxxxxxx";
$db_password = "xxxxxxxxx";
$db_database = "xxxxxxxxxxxxxx";
$link = mysqli_connect($db_host,$db_username,$db_password) or die("Cannot connect");
mysqli_select_db($link, $db_database) or die("Cannot select the database");
$sql = mysqli_query($link, "SELECT id, name, avatar FROM users WHERE id="'.$id.'" ") ;
while($result = mysqli_fetch_object($sql)) {
print ' <img src="'.$result->avatar.'" alt="'.$result->name.'" width="200" height="300" />';
}
?>
</p>
after viewing at the browser, check the url src of each image, is it at the right path? and make sure the image is existed at the src path.
i will not show you a code or an example but this sugestions might be useful :
what does the avatar column in db takes ? does it take the image name or name.extension
or location/name.extension
if it takes only the name : then you must define the location in php example
echo '<a href="#" > <IMG src="location/'.$avatar.'.jpg" alt="..."
hight="200"width="200"/></a>';
but note : in this case all the avatars must be the same extension ( *.jpg )
if it takes the name.extension then you have just to define the location only
if it takes location/name.extension then you problem is not the $avatar
but still can be the avatar's name itself be sure to do not leave avatars names contains
white spaces or non english alphabet characters. you have to renam them.
I am having trouble displaying images from my db, when i show it without the header i get the image data but when i give it the header('Content-type: image/jpeg'); nothing is displayed.
in my db i have the image column as a longblob.
$host='******';
$user='******';
$pass='******';
$db='******';
$tbl_name='******';
//connect to db
mysql_connect($host, $user, $pass);
mysql_select_db($db);
$id=addslashes($_REQUEST['id']);
$image=mysql_query("SELECT * FROM $tbl_name WHERE id='$id'");
$image=mysql_fetch_assoc($image);
$image=$image['image'];
header('Content-type: image/jpeg');
echo "<img src='$image' />";
?>`
Remove the img tag. You want to output the raw data only:
echo $image;
Note that addslashes() is not adequate protection against SQL injection. You would have to use mysql_real_escape_string() (or intval()).