Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Tutorials > PHP Tutorials

PHP Tutorials PHP Tutorials

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-29-2008, 08:52 AM
Jaan's Avatar   
Jaan Jaan is offline
Mod
 
Join Date: Dec 2006
Location: Estonia
Age: 17
Posts: 935
Last Blog:
AdStar Ad Control Pa...
Rep Power: 17
Jaan is just really niceJaan is just really niceJaan is just really niceJaan is just really nice
Send a message via MSN to Jaan
Default Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

Okay..I'm going to show y'all how to show your images.. those which are in your DB It's very very simple.. so..

First of all we must connect to our database
PHP Code:
<?php

$username 
"";
$password "";
$host "localhost";
$database "";
$username = ""; - It's your database's username
$password = ""; - It's your database's password
$host = "localhost"; - It's your database's host, usually it's localhost but it can be something else also
$database = ""; - It's your database

Now let's connect to the database

PHP Code:
@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()); 
@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error()); - This connects to your database

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());
- This will select your database

Now let's get our id

PHP Code:
$id $_GET['id']; 
It will get a id from an URL. blabla.com
So.. your id will be 3 and it will show an image which id is 3

PHP Code:
if(!isset($id) || empty($id)){
die(
"Please select your image!");
}else{ 
if(!isset($id) || empty($id)){ - If this ID in your url is empty like ?id= of if it's not even set
die("Please select your image!"); - lets display an error
}else{ - But if it is set let's continue with showing our image



PHP Code:
$query mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row mysql_fetch_array($query);
$content $row['image']; 
$query = mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'"); - Now let's select the blob from the table where id is $id (for example: 3)
$row = mysql_fetch_array($query); - Let's gather our info about image which id is $id into one variable
$content = $row['image']; - Get's the blob from our table

Now let's display our image

PHP Code:
header('Content-type: image/jpg');
echo 
$content;


header('Content-type: image/jpg'); - This tells to the browser and to the server that this file will be a jpg file
echo $content; - This will display our blob..
} - Ends else

Okay.. now here's our full script.

PHP Code:
<?php

$username 
"";
$password "";
$host "localhost";
$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());

$id $_GET['id'];

if(!isset(
$id) || empty($id)){
die(
"Please select your image!");
}else{

$query mysql_query("SELECT * FROM tbl_images WHERE id='".$id."'");
$row mysql_fetch_array($query);
$content $row['image'];

header('Content-type: image/jpg');
echo 
$content;

}

?>
It works perfectly


Enjoy
If you have questions then please feel free to ask
Attached Files To view attachments in this forum your post count must be 1 or greater. You currently have 0 posts.
__________________


Cheap & Professional Web Design | Need help? Send a PM

Last edited by Jordan; 05-07-2008 at 01:02 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-29-2008, 06:39 PM
John's Avatar   
John John is online now
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,467
Last Blog:
Joomla! And Incompeten...
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

If `id` is always to be an integer, it would be wise to add:
PHP Code:
if(!is_int($id) {
die(
"That image is not valid.");

That way your code is not nearly as vulnerable to SQL injections. I would also add mysql_real_escape_string($id) inside the query too - but not absolutely necessary. Other than that, nice tutorial.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-14-2008, 05:28 AM
danchoivt danchoivt is offline
Newbie
 
Join Date: May 2008
Posts: 1
Rep Power: 0
danchoivt is on a distinguished road
Default Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

this is cool script, thanks mod too much
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-14-2008, 05:32 AM
Jaan's Avatar   
Jaan Jaan is offline
Mod
 
Join Date: Dec 2006
Location: Estonia
Age: 17
Posts: 935
Last Blog:
AdStar Ad Control Pa...
Rep Power: 17
Jaan is just really niceJaan is just really niceJaan is just really niceJaan is just really nice
Send a message via MSN to Jaan
Default Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

you're welcome
__________________


Cheap & Professional Web Design | Need help? Send a PM
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-05-2008, 06:11 PM
ClassicGlory ClassicGlory is offline
Newbie
 
Join Date: Jun 2008
Posts: 1
Rep Power: 0
ClassicGlory is on a distinguished road
Default Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

Jaan,

I loved your easy to follow instructions but I want to do something slightly different. I have a MySQL db and want to display a Blob image from it into a table populated by PHP.
I have a MySQL query which selects all records from the database and then displays the text content from each record on a row in the table.
I want one of the table cells in each row to display the blob image.

How do I do this?

Regards

CG
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 06-08-2008, 05:59 AM
Jaan's Avatar   
Jaan Jaan is offline
Mod
 
Join Date: Dec 2006
Location: Estonia
Age: 17
Posts: 935
Last Blog:
AdStar Ad Control Pa...
Rep Power: 17
Jaan is just really niceJaan is just really niceJaan is just really niceJaan is just really nice
Send a message via MSN to Jaan
Default Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

I believe.. then you should create a real images from those blobs.. use this tutorial to create those images:

Create files with php

there.. this blob will be this $content.. so replace it..

and then.. after your script ends.. just delete those images.. then just use

PHP Code:
unlink(); 
but..when you're creating a name for your file.. use

PHP Code:
uniqid(); 
then when you have lot's of users it won't delete someone else image

i believe it works
__________________


Cheap & Professional Web Design | Need help? Send a PM
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-12-2008, 03:15 AM
ReekenX's Avatar   
ReekenX ReekenX is offline
Learning Programmer
 
Join Date: Jan 2007
Posts: 79
Rep Power: 7
ReekenX is on a distinguished road
Send a message via Skype™ to ReekenX
Default Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

Well, great tutorial. +rep
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 06-26-2008, 10:43 PM
Asinox Asinox is offline
Newbie
 
Join Date: Jun 2008
Posts: 1
Rep Power: 0
Asinox is on a distinguished road
Default Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

Hi everybody, sorry with my english... this is my first post here....
i hav a question

¿how ill show all images from my DDBB where i hav user ID?

this tutorial show just 1 images ....but if i hav a lot of picture and i need to show all of the USER ID x?

im trying to do a while but i cant do this work

Some Help?

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 07-11-2008, 03:02 AM
coated_pill coated_pill is offline
Newbie
 
Join Date: Jul 2008
Posts: 12
Rep Power: 0
coated_pill is on a distinguished road
Default Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

i have a script to show thumbnails in table but the image become corrupted in the net.. how would i solve this problem?? please help me thank you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 07-11-2008, 03:03 AM
coated_pill coated_pill is offline
Newbie
 
Join Date: Jul 2008
Posts: 12
Rep Power: 0
coated_pill is on a distinguished road
Default Re: Tutorial: Storing Images in MySQL with PHP / Part II / Display your images

if you need the script i would gladly show you .. i was wondering if i did the right thing on the MySQL dbase
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial: Storing Images in MySQL with PHP Jordan PHP Tutorials 13 09-23-2008 07:11 PM
Tutorial: PHP to MySQL Jordan PHP Tutorials 9 09-05-2008 01:12 PM


All times are GMT -5. The time now is 05:14 PM.