April 28th, 2013, 05:05 PM
Insert image in mysql with PHP
1. In MYSQL:
CREATE TABLE Photos
PhotoID int unsigned not null auto_increment primary key,
Filename varchar(255) not null unique,
Caption varchar(255) not null,
Photo longblob not null
2. Transferred an image(1.png) from C drive to hosting server
3. In PHP:
Once i run php, nothing is inserted in db.
mysql_connect("...", ".......", ".........") or die(mysql_error());
mysql_select_db("......") or die(mysql_error());
mysql_query("INSERT INTO Photos8 values
'MySQL logo by Lancioni OCP',
April 28th, 2013, 05:26 PM
1. Is MySQL running on that web server? Are you connecting to localhost?
2. If it is and you are, LOAD_FILE() needs a full path to the file.
3. If that still doesn't work then use mysql_error() to find out what the problem was.
April 28th, 2013, 05:30 PM
"nothing happens" isn't a very useful error description. When "nothing happens", it's time to start investigating: Is there a MySQL error? If that's the case, what does it say? Does the file exist? Are the database privileges and file permissions set correctly? etc.
Also note that the MySQL manual specifically says that you need to specifiy the full path to the file.
Apart from that, I question this whole approach. Why do you wanna store the images in the database? Why do you use this LOAD_FILE() stuff? You do realize this can lead to massive security issues if not used correctly?
April 28th, 2013, 05:59 PM
Hi, I simply want to add image in mysql.
I have a hosting account with www.mycheaperdomain.com.
I connected to my hosting account using filezilla.
I created a php file. In php, I connect to mysql database in hosting account through database id and pw.
April 28th, 2013, 06:20 PM
The question is: Why do you want to store the actual image content in the database? The usual approach is to store the image in the file system and merely put a link into the database. If you're not going this way, you should have a reason for it. Putting images into a folder is trivial. Putting them into the database isn't and comes with several problems, so don't do it unless you know why.
Originally Posted by phpstudent
For example, if you screw up the database privileges, people might be able to load arbitrary file via LOAD_FILE(). Not cool!
April 29th, 2013, 05:13 AM
i am surprised no one mentioned that he shouldn't be using the old MYSQL extension and instead switch to mysqli or PDO..
oh wait i just did that now
April 29th, 2013, 08:29 AM
I have used this code many times to upload image and I damn sure definitely going to help you.
$imagesql = "INSERT
INTO Image (ImageFile)
$insert = $mysqli->prepare($imagesql);
//Dont pass data directly to bind_param store it in a variable
//Assign the variable
$img = 'ImageFiles/'.$_FILES['fileImage']['name'];
April 29th, 2013, 09:00 AM
it might but the OP needs to modify it as he is trying to store an image into db (which is not a good thing to do btw)
Originally Posted by Aaliyah Byrne