Hi,

I want to show the image as well as printing the id, name, time etc with it. How can I do it?

First code prints all the data as it is without showing image.
Second code shows image without other info.

Thanks


This code returns binary code of image.

$connection = mysql_connect("localhost", "root", "");
mysql_select_db("test");

$id=3;

$s = "SELECT image_id, image_type, image, image_size, image_name, image_date FROM image WHERE image_id='$id' LIMIT 1";
$sql = mysql_query($s);

if(mysql_num_rows($sql) == 1){
	$new = mysql_fetch_array($sql);
	echo "ID: ".$new["image_id"]."<br />";
	echo "TYPE: ".$new["image_type"]."<br />";
	echo "SIZE: ".$new["image_size"]." byte<br />";
	echo "DATE: ".$new["image_date"]."<br />";
	echo "NAME: ".$new["image_name"]."<br />";
	echo "IMAGE: ".$new['image'];
} else {
	echo "ERROR: No record found";
}

This code prints image.

$connection = mysql_connect("localhost", "root", "");
mysql_select_db("test");

$id=3;

$s = "SELECT image_id, image_type, image, image_size, image_name, image_date FROM image WHERE image_id='$id' LIMIT 1";
$sql = mysql_query($s);

$new = mysql_fetch_array($sql);
echo $new['image'];

This is sql dump

CREATE TABLE IF NOT EXISTS `image` (
  `image_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `image_type` varchar(50) NOT NULL DEFAULT '',
  `image` longblob NOT NULL,
  `image_size` bigint(20) NOT NULL DEFAULT '0',
  `image_name` varchar(255) NOT NULL DEFAULT '',
  `image_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  UNIQUE KEY `image_id` (`image_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `image`
--

INSERT INTO `image` (`image_id`, `image_type`, `image`, `image_size`, `image_name`, `image_date`) VALUES
(3, 'image/pjpeg', 0xffd8ffe000104a46494600010100000100010000ffdb004300090607080706090807080a0a090b0d160f0d0c0c0d1b14151016201d2222201d1f1f2428342c242631271f1f2d3d2d3135373a3a3a232b3f443f384334393a37ffdb0043010a0a0a0d0c0d1a0f0f1a37251f253737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737373737ffc0001108006f006f03012200021101031101ffc4001b00000301010101010000000000000000000405060302010700ffc400361000020103030204040406020301000000010203000411051221314106135161142232817191a1b1234252c1e1f072d115163362ffc4001a010002030101000000000000000000000003040102050600ffc40028110002020202020201040301000000000001020003041112213141052213517191a11432c1f0ffda000c03010002110311003f00afbabed26dcb096ee30c0fd2adb88fca975df8a34b453e5a4ae3d58841fad485c5b5843133cb6b28553cff0015864fa0f535cc56ba75adb79da8b6f9df98ad1097dbf6fee6b195f9789bb460fe4edbc4a0ff00dd6c1c18c9f2cf620161f738ada2d7adddcb6f578b19de8d9e7f0c57cdeef26776f24c209c84231814769f71141010156466fab8390073838edef4adff00194b92feccd7b302aaaae4bbea5578c273a8e83e659c85a30dbd8654602f5f7cfb739af9fd83ee9d3cb432313c0048fda9d491d84fe208118bdbda4bbd80077155dbcfe383fbd7973651c092268a10cd349e5ac934815883d9476ff7d29ec4ad71aa15afb9c7665156512e8ddefc7bd7eb1949aa44d65f08f1445c670c8410c7af7fc7f41d2912b219372e4f6000e7f2af4689abeff28d8cd91fd23207dc71daaafc31e139219e3bed508deb865801ce0fa93fdabd76563e22162ddfe932ff00c4c8c9b06c78ebf612cb4f88c1a75b407ea8e25523dc0a65123c4410718ea33585b47f36f61950727debbbb9bc881a767000c923d31d4d70649762dec99d485e2a107a88fc4b7f0e9f74929405ca150c7f9573daa4ee3c477bf1045bc1234232012f82deff00850d75acdb7895ccc971e573fc3ddfd3ef5aa68b78c1648a44b900f48db91f98e6badc5c614d4ab68fb099d8f6d2d7b3f2f7a85c7af6ac402913a8c64fcd9a2e3d6f58eaa8cdf6cd716f6f32000c3316f7147c16f36dc85da070771007eb546b789e84d367a87961fd48cf125ecb2dd470f0a22193b78f9aab7c28fa2e976a26b9293de38c92dc9fcea0b5890bdfc9ec71434333c4e1d0e08ad4aebd5638ce8ce387a457bd4aaf1247f1e5a6822c8392028e952e85a298024a303827a11545a3f88511d22ba8860f1bab4f165bdacd02de5b6d0c3838ef509b5fab49aac6ac8a9875067d02f96d52e6da41244c32481938a1522bb95a38cc04c69c1209da79cd17e1ef12be9b6cf6b70a6484fd07a943541a5f8934e16c524201dc4f4ed50c187462c6a34d8ce2b049f620fa36a52dadd082e65f3626e03372c87f1ee3f1ab3814646ee99e95f32f12ea303ea11cf60c38fab1de9d59f8ceda2d390dc36265f9760e49c743587f21f1b63e9ea1bdf981c8c72ca1d46b73e8534d147f26e0a8075a85f15789d02cd6ba7932e46c7907d007719fed53bad7886eb53917ce263b73c79418e5863a923f6a56b6935c47e602719daa31fa0039fd3144c3f8915b0b6ef3fa49a3e3942eedf701114692492c63cb3236e2abd01f6a616979a946cab6f25c0e33845c9c7af4e95a5cdb2803658cf6b2c6a8082c5fcc3df1c719f4aeae0f93e50b89a494152ab1b3e0842463195200ff00a35bccc1876370ab8788a7eb50fe04ea3f126a6bf55cef1ffed47f6c51b1eb06651e74b82172ea0f4e7d7f2aca1d12d9adcccb703a756e80fae72296de594d08596553b251bd19b8f3173f50ee4668212963d08a66fc46265a154011bd103fe4e357529a8dc2b7673418a77e298105dadcc3ca48a01f9812187e1ed8a4668d4372ac19af4d81eb0c2741ab76ba964411bbb14f4a1b35f81e68a57709ca56f88adacc68b69343859460607718a95aee4b89648d63790945fa41ed59135444206a454382e89dcf49ac4221bc12150582e067b75ae8b000935b68d6336a575845f972327d067145d6845ef752403fbff11a6976d0dc9c3212f8c6d2b9e7fea9f4714a91986d624830086206e3c1033c51ba3e8e2ce06728abc80cc06588ce0e33c7fbf6a49aa5ccba8624581edd210404258ef624151918258e0fb60526ca19bb82fcbf95b43c4d6eedeee50526daf0a8c3303b770ebcf7ea0506d6d6310d9736e634203068df77f919c533f0e5c5fcdb2d97cb4b4c333ef63f28e4e54f51c903bf5ae0d941b6418886e3c92b86f6f6c579805eb7243e98a9fea25965d3ee219635b697cf12830b4728da23e32307a9f4e9d79af60bb45100899de38d1b2970785c91d36e0fa734c6e74a3e48bcb1c82abbc11d08ec79a590c12df3bcd23c4a7f9cb00bfa77e6ae750d595237bea36b9b4b68a12b792948c8fe6ebf61dea5e4886f22370573c374cd5edbf85ee5a337f7aa256619c374153bac5a6ddefe447160fcbb4f5a0d2e50e8fb82c315aed036e213111fcc2b92ac3de8cb8b7685901747de81c146ce33dbf1a36c7479aee32e8467b0a68da00d98eb2a01b2751196c572d26075a32fecde095a39176b8e99a4529999dd2418c7f28a3210dd899d979071c03adefc4d9267bbbc8ede019527e6f7ab3d3264d2e311da3092771fc4720055f5c1e84f51cfbfae291e87a75bd9c88f3ca1a6703009dab8ebd4f6edfef2eee54cb6f3a6f89d428d81492140ce08e075f519eb54b0efa1058e8e46eef27ff6a37d22e675f38ef6984b2b47969b050e724640f6ce460727a73406a36f7535f35ce9eed132c6aa46f395c0fdbed59d9d93412346b7ed951b0f971e793d813c6383cfa0fcf4d375996ca509711f9c88a08f3582b0f607e96e9c74a07bd8870814964ee3fb3b2b42613ba34686058cc6b1e3180324838e4fafa9a4baa3add399e3282d23758ca89305f07951c7031c7e3eb4c2db50b6bfb931cd1cb0c87f89ce0161d3e561f950b7730b2892c7c951b0995642f804e7d00ebdb143e241ee0d032b77e60169752dacb25bb5a865c902de67fa0374c1cf5f5e3f5cd2fd4cdc5b3f62858955072173e94ce3950081aea0b74b4f348f392321a4623a127a81c1e7f3eb5aea50d85c491428e521652ae63f98860739eb560fa3dc387e27604b4bbd5bcfb210c3dc76a80d5edee5a5667c94f4f5a69e13bb1731347330dcbfb53cbe86010331c118a17d81d9f5015918b615027cdd494906eedc10476ab2f0ac1163cd8a51ff00126a6ee62f8aba75b6ce3a1f7ade2b1d46c10cd03118ea077abb306d6fcc7efd589ade898cfc6f1c0c1244da2453cfad45ba289165da0b251b7d7935dc9ba73961429008a62a0544a8a00a856ddc274fdd24e1c491c6225dc59ce06076f7354291bdcd9c4c2cc0b6dcc536b025988e323ae47600e4f7a9cd296d24b844bade0138e2a9f5af320d32d96c818a18a4fae338c1e7fcd0ed6d30104df6651039cdcdc194d84aab03e015618618e06475ce7b77c77a22def967b9f87b8446440a19ed91fe900e5719c6793ec39eb4c3c3b6962d6cfa8adda8922ba0a2dce002bd0311dcf3d6996a3756762775edb6ef30055dab85cfa96c607ee6865f6788102d600dc40dc4f1da5a196c96d3ccf8807699d890cfce7e6524e0e33df1f7e06fad5b916a19c9631919939e7f2c50d75aa18646b48ad62dd228d8c242e383c0f941248207144d8dcfc769c19f8959195c73c30edcf7a9ef5b90430d31f12654c7757e8ecacd1a0dc11989de07e278fbd1774c8d3af918058962809eb8f4e9f7a5ab22db5c3855dc47d3e8457a6e9dee03c9b40552172e147e75765dc6d8041cbd098d9de4b6926f89b07b8f5a3ae35dba9a0316eda0f5349ebdcd14d609d9852149d912bfc31144d087620bb673eb4db56bd82ded980dbd2a0ad6f26b670d0b9535dde5ecb747321fb52e693ca01b18bd9c89ea0f336f919ba64935c0c9380093d80ef5e1359c923ab0f2890c0e770ed4d81d6a16cb020dceac59c4a10019cf21867f4a7916b375e52da178cc4c47cac9c039e33f6e69668d6e6e2ec07e067e63cfe66a988d3a06479a35c2284f9538639c8ce075e3f4a5af2a5b446e2e8c0280c373c9f48bb6b9b99b6a3c684b6e8d82ac871d71e944ea177f1312a493a0880dc90e481c0e8cb8c01c1f53f6e69bda4d1cb6656327648b8c740739c1a436ba7b2595c493b15b9865626e04d891481f4edc7248e464fb71cd0d1f63b3021f97fb7a81595cacfa87c31b759239d8ef026214f079c8f4ea4fe94ce5b2f808ae6f206431ee3b893c1241ce076f6fdeb3d3f4df87b5176e64f8b950ca21d984d8dc75ed91e9ea0703aaad56ea358decadee888c6370704f3df18cf7fd85135b3a108cca5b60f43cc5323ee90b8392dd7da9ad8d9a5d0533aafcab8031fe680d3a06f894900470b9e241f2938f4cd55db860802b22003a0354c9b38800415f7f21c40ea44e6bccd3fd63409ad998c4a09079048a40e93a12248b1ff160698aed5b06c1855cdacf9ea7a0d7b9e2b16723f91bf4ae55e490e1131ee48a2ea49cda87b9a3301dc0cf4a1617f2e72cc37ab7d601e7ed4ced74b9e4f9bce6e7d42d13ff0084963f9c4c73d71b56a86d45e8999f7dd65ac08e808d3438e35b46942338771b8a7076e3a0cf1dfa9ae92f162d42d6f2e2310c3111b8a0cb48ac5f0707fa70573d7f6acf4e4bdd3e393cb09346dcf96f8033ebfefad7574d6f716c5a49a52a8bff00c572aa304923f7ee7ad2c082c6355b723dfb996977ff000e658d372c6ee4c48c33f2f38fed59dcdc79fa8dbcb1c6559480dc64eecf5feff9d0f34f6b2436eade72346adb88e76ff4e3d680b8be95e677819cb336773375f4fc6aeb5ed8b08cb3257f66f328b54d69ed6d85bc52933b0fe2396ce7fdc9fc739a410a96393b4e7ae4d67124d272db493d734e2cad0a805845b8e0f20d4b115888bd9d680d09ad8c09c15284e39e73fda9a46cc38511103fdf4ac6187e50c12123b601ff00aaef907e78e2c7ae7fc520edccc58b4fffd9, 3666, 'tomato-jpg.jpg', '2009-11-26 14:24:01');
Member Avatar for nileshgr

You cannot display text and image both at the same time unless you use a frame on the page.

To display just the image do this. No other header outputs must go before this -

If you are using ob_start somewhere in your code, then headers can be delayed. I suggest using ob_start.

<?php

header('Content-Type: image/jpeg');

print $image;

?>

Replace Content-Type and $image with respective variables.

Sorry but to put images/files into the Database, this is the most stupid thing what you can do. FS should take care of it, to the DB! Bad Design!

Member Avatar for nileshgr

Sorry but to put images/files into the Database, this is the most stupid thing what you can do. FS should take care of it, to the DB! Bad Design!

Yeah its a bad design. But its needed sometimes. Even I was thinking of this in my VB project. But dropped the idea.

Guys do you mean, storing an image in database is bad design?

Member Avatar for nileshgr

Guys do you mean, storing an image in database is bad design?

yep man. database is not meant to store images or binary data. it doesn't make sense. why database evolved was is record keeping and reduce redundancy.

I think You cannot display text and image both at the same time unless you use a frame on the page.

That make no sense, of course you can!

I never stored an image in my database before but wanted to know how it is done. Thus, i did this example. I normaly store images in folders and then store paths to them in database.

Anyway, I know not to worry about this anymore.

Thanks for sharing your knowledge with me.

Member Avatar for nileshgr

That make no sense, of course you can!

correct me if i am wrong:

1. images can be shown on an html page using img tag only (or parameters to other objects like buttons).
2. image is binary data.
3. Content-Type header can be sent only once.

Give me an example how would you display an Image from php (binary from some source like db) and also print text on the same w/o using frames.

I never stored an image in my database before but wanted to know how it is done. Thus, i did this example. I normaly store images in folders and then store paths to them in database.

Anyway, I know not to worry about this anymore.

Thanks for sharing your knowledge with me.

and you should keep it this way

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.