简体   繁体   中英

Images store in mysql database

I want to store images in mysql database. images sends through the android app.The problem is I really don't know which method to used to store images in msql database.

 1. Access image from file
 2.Store image in filesystem and store the url in the database
 3.Store the image in the database

After storing these images.I want to get these images into PHP file.

I want to know which method is best and how to do that?

Yes, you can store images in the database, but it's not advisable and bad practice.

Do not store images in the database. Store images in directories and store references to the images in the database. Like store the path to the image in the database or the image name in the database.

Images can get quite large 1MB >. Even if it's a small image, it's still bad practice. You're putting extra hits on your database transactions that you can completely avoid. No big websites stores images in their database. For example: Facebook doesn't do it. It's not a good idea.

Avoid it. Use directories.

first create a photo table to store the reference of the image

    CREATE TABLE IF NOT EXISTS `photos` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `img` varchar(255) NOT NULL,
  `sound` varchar(255) NOT NULL,
  `about` text NOT NULL,
  `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `user_id` varchar(255) NOT NULL,
  `likes` int(255) NOT NULL,
  `down` int(255) NOT NULL,
  `seen` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

then in the upload process after saving the image to the folder get the image filename and store it to the table like below.

$save_image = mysql_query("INSERT INTO photos VALUES('','$originalFile','','$about','$data','$uid','0','0','0')") or die(mysql_error());

and then simply to display the image

    $p = mysql_query("select img from photos");
$p_f = mysql_fetch_assoc($p);

$img = $p_f['img']; 

and to display the image simply

<img src="myFolder/<?php echo $img?>">

and you done.

On a mobile platform database calls are expensive. Means it takes much more cycles to fetch from database and may degrade the performance of your application. Obviously you don't want that. Still if you want to do that BLOB is your answer.
The efficient way
add the absolute path field in your database and fetch the image from the local storage.
This is the best practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM