Jump to content

creating a function for image fetching


illuz1on

Recommended Posts

Hey ive just started storing images in a mysql database, but now I want to try and make a function that gets the image out the DB by the TITLE and prints it - like

 

<?

printimage(this.would.be.image.title);

?>

 

How would I go about this??

 

This is my table structure:

 

create table pics (

    pid int primary key not null auto_increment,

    title text,

    imgdata longblob)

 

Thanks alot

Link to comment
Share on other sites

you can do it that way... but i always prefer using id's for my indexes... doing them by name can be VERY tempermental...

 

an example of one of my superfunctions... indexing pictures by `id`... first time its run, grabs all the information, puts it into the array, every next time, it just pulls from the array, only accesses database once :D

$allpictures=array();
funcion get_pictures($id){
global $allpictures;
if(!is_numeric($id)) return;
if(!empty($allpictures[$id])) return $allpictures[id];
$query=mysql_query("SELECT * FROM `pictures`");
while($row=mysql_fetch_assoc($query)){
  $allpictures[$row[id]]=$row[url];
}
return $allpictures[$id];
}

Link to comment
Share on other sites

if ya say so... might cause you some more problems in the long run... but... your choice :-)

 

$allpictures=array();
function get_picture($title){
global $allpictures;
if(!empty($allpictures[$title])) return $allpictures[$title];
$query=mysql_query("SELECT * FROM `pictures`");
while($row=mysql_fetch_assoc($query)){
  $allpictures[$row[title]]=$row[url];
}
return $allpictures[$title];
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.