could you please demo or give example to store and retrieve image in MySql?
Title
Question
hello sir,
Through out the second level you taught how to store data , i.e only text data, today I came to know that we can store and retrieve images and display them on our webpage if so could you please make a demo for me or could you please give me a link to do that, all using php? please.....
Through out the second level you taught how to store data , i.e only text data, today I came to know that we can store and retrieve images and display them on our webpage if so could you please make a demo for me or could you please give me a link to do that, all using php? please.....
PHP-and-MySQL MySQL-Part-4
Answers:
Hi Velu,
You can store images in the database as blobs.
Although it is easier for one to sort/delete these images,
this is not recommended since it will increase the size of the database.
You can use your file-system to serve your files at high speed by only storing the image path in database.
<a href="http://www.phpriot.com/articles/images-in-mysql/4" target="_blank" title="">Here</a> is a good article on how to store images in database as blob.
Hi,
First create a table.
Mysql-Query
CREATE TABLE storeimage ( image_id tinyint(3) NOT NULL AUTO_INCREMENT, image blob NOT NULL, KEY image_id (image_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
PHP code to store image in database:
<html>
<body>
<form action="image-disp.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" method="POST">
<input type="submit" value="submit" name= "submit">
</form>
<?php
mysql_connect("localhost", "root", "root") or die (mysql_error());
mysql_select_db("mysql") or die (mysql_error());
if (isset($_FILES['image']['tmp_name']))
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = $_FILES['image']['name'];
}
if(isset($_POST['submit']))
{
$insert = mysql_query("INSERT INTO storeimage VALUES ('', '$image')");
echo " file inserted";
}
?>
</body>
</html>
First create a table.
Mysql-Query
CREATE TABLE storeimage ( image_id tinyint(3) NOT NULL AUTO_INCREMENT, image blob NOT NULL, KEY image_id (image_id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
PHP code to store image in database:
<html>
<body>
<form action="image-disp.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image" method="POST">
<input type="submit" value="submit" name= "submit">
</form>
<?php
mysql_connect("localhost", "root", "root") or die (mysql_error());
mysql_select_db("mysql") or die (mysql_error());
if (isset($_FILES['image']['tmp_name']))
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = $_FILES['image']['name'];
}
if(isset($_POST['submit']))
{
$insert = mysql_query("INSERT INTO storeimage VALUES ('', '$image')");
echo " file inserted";
}
?>
</body>
</html>
Login to add comment