I'm learning mysql and am having tremendous trouble with this code to build an image database....
I know how to create a table and I know I need longblob for images. Not a problem. Currently I'm creating via: CREATE TABLE pics
(
picid int unsigned not null auto_increment primary key,
filename varchar(255) not null unique,
caption varchar(255) not null,
pic longblob not null
);
the "not null" in picid is giving me problems. Because next when I attempt to populate using this code:
INSERT INTO pics values
(
NULL,
'bear.jpg',
'a picture of a bear',
LOAD_FILE('C:/Users/USERS_NAME/Pictures/bear.jpg')
);
I get hit with the error #1048 - Column 'pic' cannot be null.
please help. I am losing my mind....
It's not the picid that's the problem. LOAD_FILE('C:/Users/USERS_NAME/Pictures/bear.jpg') most likely fails and returns NULL.
Not to mention, you shouldn't store images in a database. Images are files, and should be stored as such on the file system. The database should hold the metadata + the file's address in the filesystem.
See Effeciently storing user uploaded images on the file system for a good system to follow.
if you still want to continue using the blob method try this tutorial
http://forum.codecall.net/topic/40286-tutorial-storing-images-in-mysql-with-php/
Related
I am using php and intervention
What is the db format of a blob?
Is MySQL saving it as base64?
Before I save an image file to db what should I do?
Image::make('......')->encode('data-url');
Is that it?
How to store a Binary Large Object (BLOB)?
A BLOB is a binary large object that can hold a variable amount of
data. The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and
LONGBLOB.
These differ only in the maximum length of the values they can hold.
The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and
LONGTEXT. These correspond to the four BLOB types and have the same
maximum lengths and storage requirements.
Hope the following code will help you:
CREATE TABLE IMAGE_TABLE(
IMG_ID INT(6) NOT NULL AUTO_INCREMENT PRIMARY KEY,
IMG_DETAILS CHAR(50),
IMG_DATA LONGBLOB,
IMG_NAME CHAR(50),
IMG_SIZE CHAR(50),
IMG_TYPE CHAR(50)
);
This will create a table which will suit your requirement.
You may also refer the following SO answers:
Binary Data in MySQL
store TEXT/BLOB in same table or not?
Storing messages as BLOB (Binary Large Object) or ordinary text?
You can refer the official documentation here. This link and this link would be worth a read to deepen your understanding.
I am creating a form that lets users to upload their photos/ video and it will be saved into my database. May I know is there a way to get this done? Im trying to save it in the database using item_path varchar which means it looks. Example: img/cats.jpg . What can I do to make it to be added to the database? Thank you
If you cannot create BLOB column types, then I would use a TEXT type with base64 encoding on the file data. You should take care to record the MIME type of the data and it would probably be a good idea to store a checksum (md5, or similar) as well.
Your files table might look something like this
-- files table
id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
blob MEDIUMTEXT NOT NULL
mime_type VARCHAR(255) NOT NULL
checksum VARCHAR(255) NOT NULL
created_at DATETIME NOT NULL
You might have other columns that associate the file to a user or any other relevant information
Here are the size limits for the MySQL TEXT column types
TYPE SIZE LIMIT
----------------------------------------------------
TINYTEXT (2^8) 256 bytes
TEXT (2^16) 65,536 bytes (64 KiB)
MEDIUMTEXT (2^24) 16,777,216 bytes (16 MiB)
LONGTEXT (2^32) 4,294,967,296 bytes (4 GiB)
When you write to the DB using PHP, use the base64_encode function to encode the file.
If you cannot create TEXT column types, then you can forget saving the actual data in your database. You will then have to save the files to the filesystem and just store a link to the file in your database.
Or lastly, as another comment points out, you could use an external blob storage like Amazon S3 or other cloud pirate services.
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.
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.
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.
I have a number of images who's information is stored in a MySQL database. The images themselves are stored in a folder, but the database contains their URL as well as other pertinent data.
I came across the PHP getimagesize which I was thinking of using to populate the width= and height= properties of the img tags of each image. However, I have been told that the overhead for this (as there are hundreds of images) will slow down the page load substantially.
I'm curious if there is a way to use getimagesize to search out all the images from their information in the database, calculate their width and height, and then insert the data into their respective width and height fields in the database, Thus allowing a simple sql query to do the work?
Here is my database structure:
CREATE TABLE `secondary_images` (
`imgId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`primaryId` int(10) unsigned DEFAULT NULL,
`imgURL` varchar(255) DEFAULT NULL,
`width` varchar(255) DEFAULT NULL,
`height` varchar(255) DEFAULT NULL,
PRIMARY KEY (`imgId`),
KEY `primaryId` (`primaryId`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;
$image_folder = "images/";
$res = mysql_query('SELECT * FROM secondary_images')
while ($row = mysql_fetch_assoc($res)) {
$size = getimagesize($image_folder.$row['imgUrl']);
mysql_query("UPDATE secondary_images SET width='{$size[0]}', height='{$size[1]} WHERE imgId='{$row['imgId']}'");
}
Why do you want to put image size in html anyway?
This algorithm may help:
Write a query that returns the URLs and imgId of images with a width of null
For each of those images, use getimagesize() to get their image size
Write an update query to update height and width by imageid
That said, I think it would be best to skip the idea of updating when a user hits the page. At some point, the initial insertion of image data is done -- can you use getimagesize() at that time, or perhaps in a separate program which performs the scan independently of your web site?
There is no way to get size of image(s) if you don't want to use getimagesize(). I think it is fast enough when they're saved locally
I am in the process of migrating a large amount of data from several databases into one. As an intermediary step I am copying the data to a file for each data type and source db and then copying it into a large table in my new database.
The structure is simple in the new table, called migrate_data. It consists of an id (primary key), a type_id (incremented within the data type set), data (a field containing a serialized PHP object holding the data I am migrating), source_db (refers to the source database, obviously), data_type (identifies what type of data we are looking at).
I have created keys and key combinations for everything but the data field. Currently I have the data field set as a longtext column. User inserts are taking about 4.8 seconds each on average. I was able to trim that down to 4.3 seconds using DELAY_KEY_WRITE=1 on the table.
What I want to know about is whether or not there is a way to improve the performance even more. Possibly by changing to a different data column type. That is why I ask about the longtext vs text vs blob. Are any of those more efficient for this sort of insert?
Before you answer, let me give you a little more information. I send all of the data to an insert function that takes the object, runs it through serialize, then runs the data insert. It is also being done using Drupal 6 (and its db_query function).
Any efficiency improvements would be awesome.
Current table structure:
CREATE TABLE IF NOT EXISTS `migrate_data` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type_id` int(10) unsigned NOT NULL DEFAULT '0',
`data` longtext NOT NULL,
`source_db` varchar(128) NOT NULL DEFAULT '',
`data_type` varchar(128) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `migrated_data_source` (`source_db`),
KEY `migrated_data_type_id` (`type_id`),
KEY `migrated_data_data_type` (`data_type`),
KEY `migrated_data_id__source` (`id`,`source_db`),
KEY `migrated_data_type_id__source` (`type_id`,`source_db`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 DELAY_KEY_WRITE=1;
The various text/blob types are all identical in storage requirements in PHP, and perform exactly the same way, except text fields are subject to character set conversion. blob fields are not. In other words, blobs are for when you're storing binary that MUST come out exactly the same as it went in. Text fields are for storing text data that may/can/will be converted from one charset to another.