sqlite PHP:数据库扩展PHP:数据库扩展

mccptt67  于 2022-12-29  发布在  SQLite
关注(0)|答案(2)|浏览(225)

我正在尝试使用(cakephp)将图像插入到sqlite DB的BLOB列中。
我在用这个密码

$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数据,因为它应该是..有没有其他的方法来做它,或者我的代码有什么问题?

o75abkj4

o75abkj41#

这是**对标题中问题的回答,因为它在[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中以blob的形式存储图像。我也会用正则表达式而不是Xpath来抓取网页!不管怎样,都能完成任务。

fcy6dtqo

fcy6dtqo2#

这不是一个答案,但这个问题有“不清楚”,太长了,不能作为一个评论
1.您提到了CakePHP,但您的问题中没有涉及CakePHP
1.你提到'SQLite',但你使用的是mysql_真实的_escape?
请明确说明您打算使用的数据库。
关于MySQL和存储图像内的数据库;

  • PHP中的mysql_ functions已弃用不再维护
  • 我试着确定你是否 * 真的 * 需要把你的图像数据*存储在 * 数据库中,一般来说,最好把图像本身存储在文件系统中,把图像的名称存储在数据库中

阅读此问题,了解如何在mysql中插入图片:
Insert Blobs in MySql databases with php
阅读此问题以获取有关在SQLite中插入图像的答案(使用PDO):
Remote image file to sqlite blob in PHP?
下面是一个针对BLOB的通用PDO逐步演练;
http://www.phpeveryday.com/articles/PDO-Working-With-BLOBs-P554.html

相关问题