2011-03-03 42 views
0

我無法弄清楚我的SQL有什麼問題。下面是生成查詢的PHP腳本:使用PHP在MySQL中更新blob

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()); 
} 

如果出現以下錯誤:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Binary='%PDF-1.5\r%âãÏÓ\r\n37 0 obj\r<

誰能給我一些指點?

回答

1

BINARY是mySQL中的reserved word

您需要使用反引號或更改列名稱。

`Binary` = '$datafile' 
0

二進制是保留字

嘗試括在反引號

`Binary`='$datafile', 

這裏是關鍵字的列表Mysql Keywords

0

除了語法錯誤中指出另一個答案是,你不能僅僅用二進制數據將變量推送到像這樣的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接受二進制數據。