PHP:插入Blob图像到SQLite表


PHP: Inserting Blob Image to SQLite table

我正在尝试使用(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();

,它插入了一个字符集,而不是一个blob数据,因为它应该是…是否有其他方法可以做到这一点,或者我的代码有什么问题?

这个对标题中问题的回答,因为它在[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();
}

我不认为编程有什么硬性规则,在某些情况下,我在Sqlite中存储blobs图像。我还使用正则表达式而不是Xpath抓取网页!只要能完成任务就行

这不是答案,但这个问题"不清楚",太长了,不能作为评论

  1. 你提到CakePHP,但是在你的问题中没有涉及CakePHP
  2. 你提到'SQLite',但你使用mysql_real_escape?

请明确您要使用的数据库。

关于MySQL和在数据库中存储图像;

    PHP中的mysql_函数已弃用不再维护
  • 尝试确定您是否真的需要在数据库中存储您的图像数据,通常,最好将图像本身存储在文件系统中,并将图像名称存储在数据库
  • 中。

在mysql中插入图片:

使用php在MySql数据库中插入Blobs

关于在SQLite中插入图像(使用PDO)的答案:

远程图像文件sqlite blob在PHP?

下面是一个用于blob的通用PDO逐步介绍;

http://www.phpeveryday.com/articles/PDO-Working-With-BLOBs-P554.html