I am programming with PHP and using XAMPP for Apache and MySQL.
I have searched and tried for two days a way to convert an image URL to a binary variable. After this process I try to insert it as a BLOB in a SQL database table.
I have tried several approaches (not included below) such as base64(), file_get_contents(), save the image file or even cURL...
Here's a picture of the SQL database table structure:
and here's the "mind blowing psychedelic" PHP code:
<?php
// APPROACH #1
$blob = file_get_contents('http://www.domainname.com/random/page/IMAGE.png');
// APPROACH #2
$bin = base64_encode($blob);
// INSERT INTO QUERY
$stmt = $db->prepare("INSERT INTO imagezzz (id_link, imagez) VALUES (?,?)");
// APPROACH #1
$stmt->bind_param("ib", $number, $blob);
// APPROACH #2
$stmt->bind_param("ib", $number, $bin);
$stmt->execute();
?>
When the query is executed it throws no error. In fact the INSERT query is performed and a new table line is added. The first and second field (id[int] and id_link[int]) haves data but the the third field (imagez [BLOB]) is empty.
I have performed a SQL INSERT QUERY inside phpMyAdmin (by uploading a local image) and it works fine...
addslashesarbitrarily like this. It's a nearly useless function that's almost always inappropriate for the job at hand, especially SQL.