使用PHP更新MySQL中的blob
我不知道我的SQL有什么问题。以下是生成查询的PHP脚本:使用PHP更新MySQL中的blob,php,mysql,sql,blob,Php,Mysql,Sql,Blob,我不知道我的SQL有什么问题。以下是生成查询的PHP脚本: function SaveData($data,$id,$file) { $handle = fopen($file['file']['tmp_name'], 'r'); $datafile = fread($handle, filesize($file['file']['tmp_name'])); $datafile = mysql_real_escape_string($datafile); fclo
function SaveData($data,$id,$file)
{
$handle = fopen($file['file']['tmp_name'], 'r');
$datafile = fread($handle, filesize($file['file']['tmp_name']));
$datafile = mysql_real_escape_string($datafile);
fclose($handle);
$query= "UPDATE data SET Text='" . $data['Text'] . "', Binary='$datafile', Tag='" . $data['Tag'] . "', name='" . $file['file']['name'] . "',type='" . $file['file']['type'] . "', size='" . $file['file']['size'] . "' WHERE Object_ID=".$id;
mysql_query($query,$this->connection) or die(mysql_error());
}
如果出现以下错误:
您的SQL语法有错误;
检查相应的手册
您的MySQL服务器版本
使用near的正确语法
'Binary='%PDF-1.5\r%n\r\n37 0
obj\r二进制文件
是mySQL中的一个文件
您需要使用反勾号或更改列名
`Binary` = '$datafile'
二进制是保留字 试着用反引号把它括起来
`Binary`='$datafile',
下面是关键字列表除了其他答案指出的语法错误之外,您不能像那样将包含二进制数据的变量推入SQL查询。您必须首先将其转换为十六进制字符串
function SaveData($data,$id,$file)
{
$handle = fopen($file['file']['tmp_name'], 'rb'); // note the b here
$datafile = fread($handle, filesize($file['file']['tmp_name']));
$contents = bin2hex(($datafile)); // no need to escape this
fclose($handle);
$query= "UPDATE data SET Text='" . $data['Text'] . "', `Binary`=x'$contents';
// rest of the code trimmed
}
还要注意,要接受二进制数据,字段必须是BLOB