我需要将blob内容插入到由许多列组成的mysql表中

时间:2016-09-17 07:47:55

标签: php blob

我有一个问题是将blob内容插入到表中并检索它,因为我的表包含一些其他字段以及Blob字段,数据正确插入但是当我尝试获取图像时没有显示。但是,如果我使用相同的代码插入到只有2列类型id,image的单独表中。它正确检索。 //插入我正在使用:

$图像=和addslashes(的file_get_contents($ _ FILES [ 'upload_file'] [ 'tmp_name的值'])); $ image_type = $ _FILES ['upload_file'] ['type'];

  

INSERT INTO questionsidsubject_idtopic_idlanguage   ,questionsolutionimg_requiredimage_typeimage)价值观   (12,'1','1','英语','QQQQ?',   '答', '是', '图像/ JPG', 'BLOB-IMG001');

//在我正在使用的屏幕上显示图像:

$ preview = $ db-> GetRow(“SELECT * from question where id ='”。mysql_real_escape_string($ _ REQUEST ['id'])。“'”);

$ preview ['image'] ='';

但是,如果我使用2个查询 - 插入后跟更新,其中insert语句将插入字段数据,然后使用blob图像更新同一行。这工作正常。我的问题是我不能让它在一个插入语句中插入完整的数据? 这是我目前如何运行它 - 1)$ id = $ questions-> insert($ data); //后跟

2)$ imgData = addslashes(file_get_contents($ _ FILES ['upload_file'] ['tmp_name']));     $ imageProperties = $ _FILES ['upload_file'] ['type'];

$ exe = $ db->执行(“update pmd_questions set image_type ='”。$ imageProperties。“',image ='”。$ imgData。“'where id ='”。$ id。“'” );

1 个答案:

答案 0 :(得分:0)

你可以做到。建议使用预处理语句并插入它。     

    public function insertBlob($filePath, $mime) {
        $blob = fopen($filePath, 'rb'); 
        $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)";
        $stmt = $this->pdo->prepare($sql);
        $stmt->bindParam(':mime', $mime);
        $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); 
        return $stmt->execute();
    }
    
参考:http://www.mysqltutorial.org/php-mysql-blob/