我正在尝试使用(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数据,因为它应该是..有没有其他的方法来做它,或者我的代码有什么问题?
2条答案
按热度按时间o75abkj41#
这是**对标题中问题的回答,因为它在[php sqlite blob]中排名第一。这没有使用PDO,因为我在使用它时遇到了麻烦。你必须使用预准备语句,你通常不能执行一个字面SQL插入,因为对于blob来说,它的大小太大了。这是一个更新而不是插入,但基本上是一样的。
我不认为编程中有什么硬性规定,在某些情况下,我会在Sqlite中以blob的形式存储图像。我也会用正则表达式而不是Xpath来抓取网页!不管怎样,都能完成任务。
fcy6dtqo2#
这不是一个答案,但这个问题有“不清楚”,太长了,不能作为一个评论
1.您提到了CakePHP,但您的问题中没有涉及CakePHP
1.你提到'SQLite',但你使用的是mysql_真实的_escape?
请明确说明您打算使用的数据库。
关于MySQL和存储图像内的数据库;
阅读此问题,了解如何在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