Nazly's BLOG

Storing Images in MySQL

Posted by Nazly on Thursday, 30th June 2005 11:12:30 (GMT +0530)

I just wrote a quick example that explains how to store images in MySQL database using the BLOB fieldtype which can used to store Binary Data. But I have been used to store the images in the filesystem and have a reference to it in the database. Most of them feel storing images or other binary data in the database is a bad idea as it creates too much overhead. But there are some advantages using this method as well. Have fun..

----- Table Structure -----
mysql> CREATE TABLE `imgtest` (
    -> `id` int(10) unsigned NOT NULL auto_increment,
    -> `imgstr` longblob NOT NULL,
    -> PRIMARY KEY (`id`)
    -> );



Insert image to the database

<?php
#Image Path
$imagePath="/path/to/img/img3.jpg";

#Connect to MySQL
mysql_connect("localhost","user","******");
mysql_select_db("test");

#Read Image file into a String
$imgStr=addslashes(file_get_contents($imagePath));

#Store Image String to the database
$query="insert into imgtest (imgstr) values ('".$imgStr."')";
mysql_query($query) or die(mysql_error());
?>




Display image from database

<?php
#Set Content Type
header("Content-Type: image/jpg");

#Image Id
$imgId=(isset($_GET["id"]))?$_GET["id"]:1;

#Connect to MySQL
mysql_connect("localhost","root","*****");
mysql_select_db("test");

#Output Image String from database
$query="select imgstr from imgtest where id='$imgId'";
$result=mysql_query($query);
$imgStr=mysql_result($result,0,"imgstr");
echo 
$imgStr;
?>


Comments (0)

Not at my best

Posted by Nazly on Monday, 27th June 2005 21:13:03 (GMT +0530)

I have been not at my best lately after my visit to Kandy last weekend. For the past week I have been struggling with cold, runny nose, fever and sore throat. Though these got cured the flem seems to lie all over my head. I'm not sure whether to call it a flu coz the symptoms are not enough closer to the definion of a flu [LOL]
I did take some medicine and now I'm more in a recovering stage as things seems to be getting normal after I started to take some antibiotics. Feel really tiring and lazy these days. Didn't play cricket this weekend which I rarely miss. Hopefully things will be back to normal soon..
Comments (2)

A day meant for records

Posted by Nazly on Wednesday, 22nd June 2005 06:41:29 (GMT +0530)

England made the most out of the match against Bangladesh yesterday when they scored a massive 391 for the loss of 4 wickets. This is the highest score ever by England and the second highest by any team in a One-day International.
Marcus Trescothick and Andrew Strauss got England to a flying start with a partnership of 141 in just 16.1 overs for the first wicket. Strauss ended up with scoring 152 in 128 balls, unfortunately he got out to the ball before the last ball of the England innings. Fireworks came from Paul Collingwood who scored 112 in 86 balls adding a 210 run partership with Strauss for the 4th Wicket.
In reply Bangladesh only managed to get 223. It was Collingwood's day as he got 6 wickets to become the first player in the history to take 6 wickets and score a century in an innings. The debutant Chris Tremlett got 4 wickets and came close to get a hat-trick on his debut when the hat-trick ball to Mohammad Ashraful bounced off the top of the bails and landed safely. Ashraful with that piece of luck ended up scoring 94 in 52 balls. His half-century came in just 21 balls.

http://www.nazly.net/paulcollingwood.jpg
Paul Collingwood's record breaking performance - A century and 6 wickets in an innings


http://www.nazly.net/christremlett.jpg
Chris Tremlett came close to a hat-trick on debut, but the bail was glued to the wicket
Comments (0)

Pieterson explodes!!!

Posted by Nazly on Sunday, 19th June 2005 23:03:16 (GMT +0530)

Kevin Pieterson's 91 not out off 65 balls against the Aussies, powered England home in the 3rd match of the Natwest Series. In reply to the Aussie total of 252, the Englishmen lost wickets and were finding it difficult to keep up with the run rate. But Pieterson quickly reacted to make sure that they got home safely with few overs to spare. During the Aussie innings Steve Harmison got 5 for 33 to restrict Australia to a moderate total.
Its the consecutive defeat in the Natwest Trophy for the Aussies as they lost to Bangladesh yesterday. Aussies are yet to score a win this season. Its also a consecutive defeat for the Aussies against England in One-day Internationals as they lost to England in the Champions Trophy Semi Finals last year.

http://www.nazly.net/kevin_pieterson.jpg
Kevin Pieterson smashes Aussie attack around the park
Comments (0)

Britney on top

Posted by Nazly on Sunday, 19th June 2005 22:16:32 (GMT +0530)

Britney Spears holds the number one spot amongst the names of the celebrities used to spread computer viruses..
Click Here to read more
Comments (0)

Biggest Upset

Posted by Nazly on Sunday, 19th June 2005 17:52:39 (GMT +0530)

Matters got bad to worse for the Aussies yesterday when they lost to Bangladesh creating the biggest upset in the history of One-day Cricket. Aussies have been dominating both forms of the game though they have made a bad start this season starting off with losing to England in the Twenty20 International and then losing to Somerset in a 50 over match.
Bangladesh restricted the Aussies to 249 for 5 wickets. Bangladesh got to that score in 49.2 for 5 wickets. Mohammad Ashraful scored a century to earn him the Man of the Match Award.
Currently the match between Australia and England is underway. Its really a tesing time for the Aussies as they are starting to panic in the run for the Ashes.
Comments (0)

Charith starts to blog atlast!!

Posted by Nazly on Wednesday, 15th June 2005 23:15:43 (GMT +0530)

My old colleague Charith started to blog atlast. You can read his blog here
Comments (1)

This is no joke...

Posted by Nazly on Wednesday, 15th June 2005 22:57:57 (GMT +0530)

It might have been a big joke when the Englishmen beat the power packed Aussie outfit in the Twenty20 match but getting beaten by Somerset in a 50 over match is no joke at all. Aussies scored a massive 342 for 5 wickets both Hayden and Ponting retiring out to give batting options to the rest scoring 76 and 80 respectively.
In reply Somerset reached the target in 46.5 overs. South African captain Greame Smith's exceptional form continued as he scored 108 in 74 balls while Sri Lankan master blaster scored 101 of 79 balls adding 197 run partnership for the first wicket.
Aussies have to stay focused as the Natwest Series begins tomorrow. England have never been better as they are at the moment.
Comments (0)

Twenty Balls of Twenty20

Posted by Nazly on Tuesday, 14th June 2005 06:46:14 (GMT +0530)

It was the twenty balls of the first one off Twenty20 match between England and Australia did the trick when Darren Gough and Jon Lewis fired with all cylinders to capture 7 wickets of a strong Aussie batting line up for just 8 runs.
England scored 179 for 8 in 20 overs. Trescothik scored 41 off 31 balls though the firing came from Pietersen[34 off 18] and Collingwood[46 off 26]. Aussie top order had no answer against Gough and Lewis as they were bowled out for 79 runs giving a 100 run victory for the Englishmen.
This will definitely be a confidence boost for England for the rest of the tour as they are concentrating hard to regain the Ashes.

http://www.nazly.net/darrengough.jpg
Darren Gough celebrates as another Aussie wicket falls
Comments (0)

Day off

Posted by Nazly on Sunday, 12th June 2005 20:58:37 (GMT +0530)

I had a day off today from my work and compy and headed towards the downsouth of the isle with my colleagues. Went to Beruwela and spent some time at the beach. Played beach volleyball and enjoyed it a lot. Had a good swim in the pool and relaxed a bit in the afternoon. Its fun + a break for change.
Comments (0)

MySQL 4.1 authentication issue

Posted by Nazly on Saturday, 11th June 2005 20:00:59 (GMT +0530)

I have been working on MySQL 4.0.17 till now and wanted to upgrad to MySQL 4.1. So I downloaded and installed MySQL 4.1 and the installation was successful and it was working fine. But when I tried to access it through PHPMyAdmin or from any of my PHP code it threw this error

Client does not support authentication protocol requested by server;
consider upgrading MySQL client


So I checked it out at MySQL.com to see what I was missing and found out that in MySQL 4.1 the authentication process has been upgraded by making the hashes more secure. So when trying to access it with an old client it throws the above error.

The mysql extension in PHP doesn't support the new authentication protocol. So the workaround for this will be to reset the passwords in the format prior to MySQL 4.1. This can be done by using the old_password() function. Make sure to only reset the passwords of the users that need to access using a client prior to 4.1. Here is how it can be done

mysql> SET PASSWORD FOR
    -> 'some_user'@'some_host' = OLD_PASSWORD('newpwd');



or

mysql> UPDATE mysql.user SET Password = OLD_PASSWORD('newpwd')
    -> WHERE Host = 'some_host' AND User = 'some_user';
mysql> FLUSH PRIVILEGES;



The mysqli extension (MySQL Improved) which comes with PHP 5 is compatible with the improved password hashing in MySQL 4.1 and above.
Comments (0)

RSS Feed added

Posted by Nazly on Thursday, 9th June 2005 16:56:05 (GMT +0530)

Atlast I have added the RSS feed of my blog. Now you can keep up-to-date when I make a new post.
Comments (0)

Happy Birthday - PHP!!!

Posted by Nazly on Wednesday, 8th June 2005 20:11:41 (GMT +0530)

It has been 10 years since the first version of PHP was released by Rasmus Lerdorf. It has become the most popular Server Side Scripting Language since then. Long live PHP!!!
Comments (0)

Hands on GD Library

Posted by Nazly on Tuesday, 7th June 2005 22:26:17 (GMT +0530)

GD Library is one of the coolest things on earth that I want to get my hands on, though I didn't get an opportunity to work really hard on it. I did some basic GD stuff to dynamically generate graphs for a website way back in the year 2002. But that was just elementary stuff.
Lately I started coding a web based image viewer and ended up with PHPThumbsUp v1.0. It simply reads the directory and list the folder under it. When you click on a folder it will display the images(JPEG, GIF and PNG) that it contains. The thumbnail images are resized on the fly using GD Libary. By resizing the images it will be loaded quickly as the capacity of the image will be reduced. I resized it with resampling so that it maintians the quality of the image. It has been good practise as I tested out few other things as well using GD Library.
One of the main reasons that I wrote a viewer that reads a directory rather than reading a database because most of the webmasters prefer to upload the images to the server in a bunch and look for a method to display them. They don't like to go through the pain of adding records to the database even by using a GUI interface.

Click Here for a DEMO of PHPThumbsUp v1.0.
Comments (0)