PHP:将Blob图像插入SQLite表

Moh*_*gab 2 php sqlite cakephp blob

我正在尝试使用(cakephp)将图像插入BLOB列中的sqlite DB.

我正在使用此代码

$insert = "INSERT INTO story (page, image) 
                    VALUES (:page, :image)";
        $stmt = $file_db->prepare($insert);

        // Bind parameters to statement variables
        $img = file_get_contents($this->request->data['image']['tmp_name']);
        $img = mysql_real_escape_string($img);
        $stmt->bindParam(':page', $this->request->data['page']);
        $stmt->bindParam(':image', $img);

           // Execute statement
          $stmt->execute();
Run Code Online (Sandbox Code Playgroud)

并且它正在插入,但是字符集不是blob数据,因为它假设是..有没有其他方法来做它或者我的代码有什么问题?

geo*_*sey 7

标题中问题的答案,因为它在[php sqlite blob]中排名第一.这不使用PDO,因为我遇到了麻烦.你必须使用预准备语句,你通常不能执行文字SQL插入,因为blob的大小太大.这是一个更新而不是插入,但它基本相同.

// example variables
$row_id=1;
$image_filename="/home/mywebsite/public_html/images/an_image.png";
$sqlite_table_name="user_images";
$database_filename="database.sqlite";
// the blob field in the sqlite table is called ZIMAGE and the id field is ZID.

// Code
if (file_exists($image_filename)) {
$db = new SQLite3($database_filename);
$query = $db->prepare("UPDATE '{$sqlite_table_name}' SET ZIMAGE=? WHERE ZID=?");
$image=file_get_contents($image_filename);
$query->bindValue(1, $image, SQLITE3_BLOB);
$query->bindValue(2, $row_id, SQLITE3_TEXT);
$run=$query->execute();
}
Run Code Online (Sandbox Code Playgroud)

我不认为编程有任何硬性规则,我在某些情况下将图像存储在Sqlite中的blob中.我还用正则表达式而不是Xpath来抓取网页!无论做什么工作.