Connect with Facebook 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, 07:52 AM
Jaan's Avatar   
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 18
Posts: 1,511
Blog Entries: 4
Rep Power: 24
Jaan is just really niceJaan 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.

Last edited by Jordan; 05-07-2008 at 12:02 PM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-29-2008, 05:39 PM
John's Avatar   
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 5,304
Blog Entries: 24
Rep Power: 20
John is just really niceJohn is just really niceJohn is just really niceJohn is just really niceJohn is just really nice
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-14-2008, 04:28 AM
Newbie
 
Join Date: May 2008
Posts: 1
Rep Power: 0
danchoivt is an unknown quantity at this point
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, 04:32 AM
Jaan's Avatar   
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 18
Posts: 1,511
Blog Entries: 4
Rep Power: 24
Jaan is just really niceJaan 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-05-2008, 05:11 PM
Newbie
 
Join Date: Jun 2008
Posts: 1
Rep Power: 0
ClassicGlory is an unknown quantity at this point
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
  #6 (permalink)  
Old 06-08-2008, 04:59 AM
Jaan's Avatar   
Moderator
 
Join Date: Dec 2006
Location: Estonia
Age: 18
Posts: 1,511
Blog Entries: 4
Rep Power: 24
Jaan is just really niceJaan 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:

http://forum.codecall.net/php-tutori...files-php.html

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-12-2008, 02:15 AM
ReekenX's Avatar   
Programmer
 
Join Date: Jan 2007
Posts: 133
Rep Power: 0
ReekenX is an unknown quantity at this point
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
__________________
WTemplates - web templates, free templates | CodeCall CooL!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 06-26-2008, 09:43 PM
Newbie
 
Join Date: Jun 2008
Posts: 1
Rep Power: 0
Asinox is an unknown quantity at this point
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, 02:02 AM
Newbie
 
Join Date: Jul 2008
Posts: 12
Rep Power: 0
coated_pill is an unknown quantity at this point
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, 02:03 AM
Newbie
 
Join Date: Jul 2008
Posts: 12
Rep Power: 0
coated_pill is an unknown quantity at this point
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
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


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


All times are GMT -5. The time now is 11:15 PM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%

Ads