php - Inserting Binary into MySQL BLOB -


$serv = "xxx"; $user = "xxx";  $pass = "xxx";  $db = "xxx";   $imgloc = "../images/bg.jpg";  $image = fopen($imgloc, 'rb');  $imagecontent = fread($image, filesize($imgloc));   $conn = new mysqli($serv, $user, $pass, $db);   $sql = "insert `image`(`advert_id`,`img`) values('1','" . $imagecontent . "');";  $conn->query($sql); 

i'm using above code try insert binary mysql database nothing being sent database. $imagecontent appears in database null if echo $imagecontent seems show binary data.

advert_id int field , img blob

the reason why code isn't working because need escape data.

$imagecontent = fread($image, filesize($imgloc));  $imagecontent = mysqli_real_escape_string($conn, $imagecontent); 

you not seeing syntax error, similar to:

you have error in sql syntax; check manual corresponds mysql server version right syntax use near '8 16@#54' @ line 1...

  • because not checking errors.

visit http://php.net/manual/en/mysqli.error.php , http://php.net/manual/en/function.error-reporting.php, use following @ top of file:

<?php  error_reporting(e_all); ini_set('display_errors', 1); mysqli_report(mysqli_report_error | mysqli_report_strict); // rest of code 

this signal syntax errors.


use mysqli prepared statements, or pdo prepared statements


plus, mike brant said in comments, , quote:

"as tangential comment, recommend making sure have use case storing images blobs in mysql. in lot of cases, might not idea when compared storing file references in database."

  • mike speaks truth. database increase dramatically on time, therefore storing copy of files in folder making reference it, better idea, entirely you.

read following q&a's on stack:


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -