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
https://forums.phpfreaks.com/topic/49577-creating-a-function-for-image-fetching/
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];
}

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];
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.