Insert BLOB

Issue #24 resolved
Paweł Cierzniakowski created an issue

Hi, how to prepare statement with blob? Example in common MySQLi:

<form method="post" enctype="multipart/form-data">
<input type="file" name="image"  accept="image/*">
<button type="submit" name="submit">Submit</button>
</form>
$null = NULL;
$stmt = $mySQLi->prepare("INSERT INTO table (string1, string2, string3, image) VALUES (?, ?, ?, ?)");
$stmt->bind_param("sssb", $_POST['string1'], $_POST['string2'], $_POST['string3'], $null);
$stmt->send_long_data(3, file_get_contents($_FILES['image']['tmp_name'])); // Insert image from POST
$stmt->execute();
$stmt->close();

Comments (2)

  1. Daniel DiLauro

    Pawel,

    I extended the class and added my own prepare method, which returns the underlying mysqli_stmt object to handle cases like this.

        public function my_prepare($query){
            $this -> stmt = $this -> _mysqli -> prepare($query);
            return $this -> stmt;
        }
    

    Which, per your example, I would call like this

    $stmt = $mySQLi->my_prepare("INSERT INTO table (string1, string2, string3, image) VALUES (?, ?, ?, ?)");
    

    The rest of your binds, executes, etc... should work normally on the stmt object (outside of this wrapper class).

  2. Log in to comment