0
<?php
$size       = filesize('H:/blog_banner.jpg');
$fp         = fopen('H:/blog_banner.jpg','rb');
$binary_pic = fread($fp,$size);
$link       = mysqli_connect('127.0.0.1','root','123456') or die('connect failed!');
$binary_pic = base64_encode($binary_pic);

mysqli_select_db($link, 'rqPro');
mysqli_query($link,"insert into tb_pic values('',$binary_pic)") or die('cant perform pic');

The result is:

cant perform pic.

The picture in mysql save field is :medium blob.

How can I solve this?

1
  • Use file_get_contents('tourpic.jpg') to retrieve image, if needed like u want do base64 encode and save to your mysql field. But better practice generally is saving reference in database, and file on filesystem, this will reduce your db export size and db load Commented Mar 3, 2016 at 6:25

2 Answers 2

1

Your insert query is wrong. You don't specify which field of table it will be saved. It should be .. INSERT INTO tb_pic(field1,field2) VALUES('',$binary_pic)

And my advice, it's not a good way to save pictures in Database. It's heavy. It's better to save it in your server / directory. Try to look http://php.net/manual/en/function.move-uploaded-file.php

Sign up to request clarification or add additional context in comments.

Comments

0

You have to quote the image string:

values('','$binary_pic') 

Also, make sure the image is small enough to fit inside a medium blob. Otherwise, it will get trimmed and you won't be able to extract it correctly

Note: if the table has more than two columns, you need to specify the ones for which the inserted data is for.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.