How to send data in blocks from a text file into a blob type field using MySQLi in PHP

1 Answer

0 votes
$db_host        = 'localhost';
$db_user        = 'root';
$db_password    = '';
$db_name        = 'allonpage';
 
$con = new mysqli('localhost', $db_user, $db_password, $db_name);
 
if ($con->connect_error) 
    die('Connection Error: ' . $con->server_info);
 
$sql = "
  INSERT INTO test_table (blob_data) VALUES (?)
";    
     
if ($stmt = $con->prepare($sql)) 
{
    $null = NULL;
    $stmt->bind_param("b", $null);
    $fp = fopen("d:\\3LData.txt", "r");
    while (!feof($fp)) 
        $stmt->send_long_data(0, fread($fp, 4096));

    fclose($fp);
    $stmt->execute();
 
    $stmt->close();
}
                                  
$con->close();
 
/*
run: 

[BLOB - 64 KiB]
 
*/

 



answered Jul 12, 2016 by avibootz
...