2011-03-03 2 views
0

SQL에 어떤 문제가 있는지 알 수 없습니다. 다음은 쿼리를 생성하는 PHP 스크립트입니다 :PHP를 사용하여 MySQL에서 BLOB 업데이트

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<

사람이 좀 포인터를 줄 수 :

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

다음과 같은 오류가 발생하는 경우?

답변

1

BINARY은 mySQL에 reserved word입니다.

역 따옴표를 사용하거나 열 이름을 변경해야합니다.

`Binary` = '$datafile' 
0

이진 예약어를 다시 묶어야하는

보십시오 여기

`Binary`='$datafile', 

는 구문 오류 외에도 Mysql Keywords

0

는 지적 키워드의 목록입니다 따옴표입니다 다른 대답은 바이너리 데이터를 가진 변수를 이와 같은 SQL 쿼리에 넣을 수 없다는 것입니다. 먼저 16 진수 문자열로 변환해야합니다.

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이어야합니다.

관련 문제