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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 图书信息管理系统程序编写
  • ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题