dongshang1768 2013-04-23 18:19
浏览 31
已采纳

在mysql中插入文件字节代码中的php错误

I'm having a big problem

when i try to put the query string in the file the following error appears

Incorrect string value: '\ XE6 \ x00 \ x00 \ xfd \ xfd \ xfd ...'

INSERT INTO `web_plugins` (`nome`, `xmllocal_nome`, `icone_url`, `icone_bytecode`, `swf_url`, `swf_bytecode`) VALUES ('asdfasdf', 'wqwerrwe', '/assets/uploads/plugins_icons/f2d2d3d9.gif', 'GIF89a\0\0�\0\0������������������������������������������������������������������������������������������������������������������������������~~~}}}|||{{{zzzyyywwwvvvuuutttsssrrrqqqpppooonnnmmmjjjfffdddbbb```___]]]\\\\\\ZZZYYYXXXWWWRRRQQQCCC@@@888���\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0!�\0\0Q\0,\0\0\0\0\0\0\0��\'KP?BI2/?\02G&-*8\'+;NL:!.\"%)\"\'&\'-=B!1)$(%&$�1H;+\',-(&%%#�!4\Z\'))�Ժ(##!94)*$#�\'*&# \"#-/\'\"��$! <>!$!!\"HP���2>���_��p���c��,�H����6F�:�Qr+4�0aF�b�L�8�� 
���b&�]��I�
  • 写回答

1条回答 默认 最新

  • douzhai1182 2013-04-23 18:22
    关注

    You should save the gif in a BLOB column.

    Imagine the following table:

    CREATE TABLE testblob(
      id INT AUTO_INCREMENT ,
      data MEDIUMBLOB,
      PRIMARY KEY ( id )
    ) ENGINE = InnoDB;
    

    Then you can use the following INSERT code:

    <?php
        $dbh = mysql_connect("localhost", "user");
        mysql_select_db("test");
        $data = file_get_contents("your.gif");
        // This is important to avoid a ' to accidentally close a string
        $data = mysql_real_escape_string($data);
        mysql_query("INSERT INTO testblob(data) VALUES ('$data')");
    ?>
    

    Note that this is taken from an article from the web. Thanks to the author.

    Further you should note, that the mysql_* extension has been marked deprecated. You should use the PDO or mysqli extension instead. I personally prefer PDO. Here comes an example that uses PDO:

    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
    
    // create a prepared statement
    $stmt = $conn->prepare('INSERT INTO `testblob` (`data`) VALUES (:data)');    
    // assign the blob value to it
    $stmt->bindParam(1, file_get_contents('your.gif'), PDO::PARAM_LOB);
    
    // execute the statemt
    $stmt->execute();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用