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

查看:59
本文介绍了使用 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双引号内,变量仍然被神奇地扩展.

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)

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


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天全站免登陆