简体   繁体   English

PHP MySQL更新blob

[英]php mysql update blob

New to PHP/mySql and having trouble inserting and retrieving binary data. PHP / mySql的新手,在插入和检索二进制数据时遇到麻烦。 I have a mySql table called usr_pressdata. 我有一个名为usr_pressdata的mySql表。 The field 'BinDat' is of type mediumblob. “ BinDat”字段的类型为mediumblob。

$dat = $this->parseOverview($sql);
    // $dat is now a binary string

$datsql = "Update usr_pressdata Set BinDat = " . $dat;
$datresult = mysql_query($datsql, $this -> conn) or die(mysql_error());

$getdat = "Select * from usr_pressdata";
$getdatresult = mysql_query($getdat, $this -> conn) or die(mysql_error());
$row = mysql_fetch_array( $getdatresult );
   $retval = $row['BinDat'];

In this example my goal is that $retval == $dat but it does not. 在此示例中,我的目标是$ retval == $ dat,但不是。 I suspect that my query string $datsql is incorrect. 我怀疑我的查询字符串$ datsql不正确。 Can someone correct this example code? 有人可以更正此示例代码吗? Thank you. 谢谢。

When inserting values in a table (or more generally, when including a value in an SQL request): 在表中插入值时(或更普遍地,在SQL请求中包括值时):

  1. the string must be enclosed between quotes ('...') 字符串必须用引号('...')引起来
  2. the string must be “escaped” using mysql_real_escape_string so as to prevent SQL injection . 必须使用mysql_real_escape_string对字符串进行“转义”,以防止SQL注入

So you need to write something like: 因此,您需要编写如下内容:

$request = "UPDATE usr_pressdata SET bindat= '" . mysql_real_escape_string($dat) . "';";

I suspect you may want to add a WHERE someColumn = someCondition clause at the end, because as it is now, it would affect all the rows in the table. 我怀疑您可能想在末尾添加WHERE someColumn = someCondition子句,因为到现在为止,它将影响表中的所有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM