Image with text

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jan2oo
Forum Newbie
Posts: 4
Joined: Wed Jan 19, 2005 2:22 pm

Image with text

Post by jan2oo »

Hi,

I am trying to create a simple photoalbum using mysql and php.
I wanted to display the images with its descripion on my webpage.

First i created a table using the following code.

Code: Select all

CREATE TABLE photo( 
id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
description CHAR(255), 
bin_data LONGBLOB, 
filename CHAR(50), 
filesize CHAR(50), 
filetype CHAR(50) 
);
Using the below script i uploded the image and description to the mysql table.

Code: Select all

<?php 
// code that will be executed if the form has been submitted: 

if ($submit) &#123; 

// connect to the database 
// (you may have to adjust the hostname,username or password) 

MYSQL_CONNECT("localhost","root","password"); 
mysql_select_db("dbname"); 

$data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); 

$result=MYSQL_QUERY("INSERT INTO photo(description,bin_data,filename,filesize,filetype) ". 
"VALUES ('$form_description','$data','$form_data_name','$f
orm_data_size','$form_data_type')"); 

$id= mysql_insert_id(); 
print "<p>This file has the following Database ID: <b>$id</b>"; 

MYSQL_CLOSE(); 

&#125; else &#123; 

// else show the form to submit new data: 
?> 

<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> 
File Description:<br> 
<input type="text" name="form_description" size="40"> 
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 
<br>File to upload/store in database:<br> 
<input type="file" name="form_data" size="40"> 
<p><input type="submit" name="submit" value="submit"> 
</form> 

<?php 

&#125; 

?>
Now i wanted to display all the images and its description which i stored in mysql to a table .Is there any way to display this in a table..?

I am new to php and checked a few tutorial to get it completed.

Please help to get it worked.

Thanks in advance,

Jan


feyd | please use formatting!
Last edited by jan2oo on Thu Jan 20, 2005 7:45 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

we've talked about storing images and other binaries in databases before, and generally agreed that it wasn't the best idea. To find out why, please read those topics found through using the following in the search field: '+image +database +blob*' minus the single quotes.

If you still want to use mysql to store the images, you'll need a script that can be requested to retrieve a single image. It would be called like this:

Code: Select all

<img src="your_download_script.php?id=312387" />
or similar.. You can find the topics where we discuss how to do things of this nature through searching for: '+force +download' sans quotes.

The problem will popup that your pages run real slow. This is due to having to make a database query for every image stored, plus the initial query or few to display the page.

enjoy.

--feyd v2
jan2oo
Forum Newbie
Posts: 4
Joined: Wed Jan 19, 2005 2:22 pm

Post by jan2oo »

Hi Feyd,

Thank you so much.As you said I tried to search the forum.But i couldnot get any way to display both image and description.

My script is working fine for displaying image,but its not displaying any description.The following is the code.

Code: Select all

<?php

if($id) &#123;

    // you may have to modify login information for your database server:
    @MYSQL_CONNECT("localhost","user","pass");

    @mysql_select_db("dbname");
	
    $query = "select description,bin_data,filetype from photo where id=$id"; 
    $result = @MYSQL_QUERY($query); 

    $data = @MYSQL_RESULT($result,0,"bin_data"); 
   $type = @MYSQL_RESULT($result,0,"filetype"); 
   $description= @MYSQL_RESULT($result,0,"description"); 

  Header( "Content-type: $type"); 
  Header( "Content-description: $description");
  echo $data; 
  echo $description;
  
&#125;; 
?>
Please help me to display the description.

Thanks,
Jan.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

The reason why the description is not showing up is that with those headers, you are telling thee browser that you are sending it a picture. Then you send it the picture data, which works ok, but then you send it the text of the description which it can't display because it's still looking for picture data, not text.

You need to do something with the img tag like what feyd said
jan2oo
Forum Newbie
Posts: 4
Joined: Wed Jan 19, 2005 2:22 pm

Post by jan2oo »

Thanks Magicrobotmonkey.

I again tried the following script.

Code: Select all

<?php 


if($id) &#123; 

@MYSQL_CONNECT("localhost","user","pass"); 

@mysql_select_db("dbname"); 

$query = "select description from photo where id=$id"; 

$result = @MYSQL_QUERY($query); 

$description = @MYSQL_RESULT($result,0,"description"); 

echo $description; 
echo "<img src="getimage.php?id=$id">"; 


&#125;; 
?>
But didn't work.Is there anything wrong on my code..?

Thanks,
Jan.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

which part didn't work?
jan2oo
Forum Newbie
Posts: 4
Joined: Wed Jan 19, 2005 2:22 pm

Post by jan2oo »

After executing that code i got a blank page.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

maybe you mean to use $_GET['id'] instead?
Post Reply