使用php在MySql数据库中插入Blob [英] Insert Blobs in MySql databases with php

查看:101
本文介绍了使用php在MySql数据库中插入Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我试图将图像存储在数据库中,因此似乎无法正常工作.这是我的桌子的结构.

I am trying to store an image in the DataBase, for some reason it doesn't seem to work. Here's the structure of my table.

mysql> describe ImageStore;
+---------+----------+------+-----+---------+-------+
| Field   | Type     | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| ImageId | int(11)  | NO   | PRI | NULL    |       |
| Image   | longblob | NO   |     | NULL    |       |
+---------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)

这是我的查询,其中插入了图像,或者至少就是该插入的图像:

And here is my query which inserts the image or at least thats what it should:

//Store the binary image into the database
                $tmp_img = $this->image['tmp_name'];
                $sql = "INSERT INTO ImageStore(ImageId,Image)               
                VALUES('$this->image_id','file_get_contents($tmp_image)')";
                mysql_query($sql); 

如果我打印file_get_contents($ tmp_image)的值,那么屏幕上会有大量数据.但是这个值不会存储在数据库中,这就是我面临的问题.

If I print the value of file_get_contents($tmp_image), then there is a tons of data on the screen. But this value doesn't get stored in the database and that is the issue that I'm facing.

推荐答案

问题

$sql = "INSERT INTO ImageStore(ImageId,Image)
        VALUES('$this->image_id','file_get_contents($tmp_image)')";

这将在PHP中创建一个名为$sql的字符串.暂时不要使用MySQL,因为您尚未执行任何查询.您只是在构建一个字符串.

This creates a string in PHP named $sql. Forget about MySQL for a minute, because you're not executing any query yet. You're just building a string.

PHP的神奇之处在于,您可以编写一个变量名—.例如$this->image_id inside 中的双引号和变量仍然得到了神奇的扩展.

The magic of PHP means that you can write a variable name — say, $this->image_idinside the double quotes and the variable still gets magically expanded.

此功能称为变量插值",不会在函数调用中出现.因此,您在这里要做的就是将字符串"file_get_contents($tmp_image)"写入数据库.

This functionality, known as "variable interpolation", does not occur for function calls. So, all you're doing here is writing the string "file_get_contents($tmp_image)" into the database.

因此,要连接调用file_get_contents($tmp_image)的结果,您必须跳出字符串并明确地执行操作:

So, to concatenate the result of calling file_get_contents($tmp_image), you have to jump out of the string and do things explicitly:

$sql = "INSERT INTO ImageStore(ImageId,Image)
        VALUES('$this->image_id','" . file_get_contents($tmp_image) . "')";

(您甚至可以仅从突出显示其工作原理的语法中看到.)

(You can see even just from the syntax highlighting how this has worked.)

现在的问题是,如果二进制数据包含任何',则您的查询无效.因此,您应通过mysql_escape_string运行它以对其进行清理以进行查询操作:

Now the problem you have is that if the binary data contains any ', your query is not valid. So you should run it through mysql_escape_string to sanitize it for the query operation:

$sql = "INSERT INTO ImageStore(ImageId,Image)
        VALUES('$this->image_id','" . mysql_escape_string(file_get_contents($tmp_image)) . "')";


解决方案(3)

现在您有一个 really 大字符串,并且您的数据库越来越庞大.


Solution (3)

Now you have a really big string, and your database is getting bulky.

首选不在数据库中存储图像,您可以在其中提供帮助.

Prefer not storing images in databases, where you can help it.

这篇关于使用php在MySql数据库中插入Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆