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

时间:2022-09-25 19:18:39

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 ...'

字符串值不正确:'\ 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\'+;\rNL:!.\"%)\"\'&\'-=B!1)$(%&$�1H;+\',-(&%%#�!4\Z\'))�Ժ(##!94)*$#�\'*&# \"#-/\'\"��$! <>!$!!\"HP���2>���_��p���c��,�H����6F�:�Qr+4�0aF�b�L�8�� \n���b&�]�Ø�I�

1 个解决方案

#1


1  

You should save the gif in a BLOB column.

您应该将gif保存在BLOB列中。

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:

然后您可以使用以下INSERT代码:

<?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:

此外,您应该注意,mysql_ *扩展已被标记为已弃用。您应该使用PDO或mysqli扩展名。我个人更喜欢PDO。这是一个使用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();

#1


1  

You should save the gif in a BLOB column.

您应该将gif保存在BLOB列中。

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:

然后您可以使用以下INSERT代码:

<?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:

此外,您应该注意,mysql_ *扩展已被标记为已弃用。您应该使用PDO或mysqli扩展名。我个人更喜欢PDO。这是一个使用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();