• <tfoot id='bZjVn'></tfoot>

    1. <legend id='bZjVn'><style id='bZjVn'><dir id='bZjVn'><q id='bZjVn'></q></dir></style></legend>
    2. <small id='bZjVn'></small><noframes id='bZjVn'>

      • <bdo id='bZjVn'></bdo><ul id='bZjVn'></ul>
    3. <i id='bZjVn'><tr id='bZjVn'><dt id='bZjVn'><q id='bZjVn'><span id='bZjVn'><b id='bZjVn'><form id='bZjVn'><ins id='bZjVn'></ins><ul id='bZjVn'></ul><sub id='bZjVn'></sub></form><legend id='bZjVn'></legend><bdo id='bZjVn'><pre id='bZjVn'><center id='bZjVn'></center></pre></bdo></b><th id='bZjVn'></th></span></q></dt></tr></i><div id='bZjVn'><tfoot id='bZjVn'></tfoot><dl id='bZjVn'><fieldset id='bZjVn'></fieldset></dl></div>

      1. 为什么在将二进制数据从 PHP 插入 MySQL 时使用 bin2hex?

        Why use bin2hex when inserting binary data from PHP into MySQL?(为什么在将二进制数据从 PHP 插入 MySQL 时使用 bin2hex?)
        <legend id='TwvtT'><style id='TwvtT'><dir id='TwvtT'><q id='TwvtT'></q></dir></style></legend>

          <i id='TwvtT'><tr id='TwvtT'><dt id='TwvtT'><q id='TwvtT'><span id='TwvtT'><b id='TwvtT'><form id='TwvtT'><ins id='TwvtT'></ins><ul id='TwvtT'></ul><sub id='TwvtT'></sub></form><legend id='TwvtT'></legend><bdo id='TwvtT'><pre id='TwvtT'><center id='TwvtT'></center></pre></bdo></b><th id='TwvtT'></th></span></q></dt></tr></i><div id='TwvtT'><tfoot id='TwvtT'></tfoot><dl id='TwvtT'><fieldset id='TwvtT'></fieldset></dl></div>
            <bdo id='TwvtT'></bdo><ul id='TwvtT'></ul>
            <tfoot id='TwvtT'></tfoot>
              <tbody id='TwvtT'></tbody>

            <small id='TwvtT'></small><noframes id='TwvtT'>

                  本文介绍了为什么在将二进制数据从 PHP 插入 MySQL 时使用 bin2hex?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我听说在将二进制数据(文件等)插入 MySQL 时,应该使用 bin2hex() 函数并将其作为 HEX 编码值发送,而不是仅仅使用 mysql_real_escape_string 在二进制字符串上并使用它.

                  I heard a rumor that when inserting binary data (files and such) into MySQL, you should use the bin2hex() function and send it as a HEX-coded value, rather than just use mysql_real_escape_string on the binary string and use that.

                  // That you should do
                  $hex = bin2hex($raw_bin);
                  $sql = "INSERT INTO `table`(`file`) VALUES (X'{$hex}')";
                  
                  // Rather than
                  $bin = mysql_real_escape_string($raw_bin);
                  $sql = "INSERT INTO `table`(`file`) VALUES ('{$bin}')";
                  

                  据说是出于性能原因.与 MySQL 如何处理大字符串与它如何处理 HEX 编码值有关

                  It is supposedly for performance reasons. Something to do with how MySQL handles large strings vs. how it handles HEX-coded values

                  但是,我很难确认这一点.我所有的测试都表明完全相反.bin2hex 方法的速度慢了约 85%,并且使用了约 24% 的内存.
                  (我在 PHP 5.3、MySQL 5.1、Win7 x64 上测试这个 - 使用非常简单的插入循环.)

                  However, I am having a hard time confirming this. All my tests indicate the exact oposite; that the bin2hex method is ~85% slower and uses ~24% more memory.
                  (I am testing this on PHP 5.3, MySQL 5.1, Win7 x64 - Using a farily simple insert loop.)

                  例如,此图显示了测试代码运行时 mysqld 进程的私有内存使用情况:

                  For instance, this graph shows the private memory usage of the mysqld process while the test code was running:


                  (来源:advefir.com)

                  有没有人有任何解释或资源可以澄清这一点?

                  Does anybody have any explainations or reasources that would clarify this?

                  谢谢.

                  推荐答案

                  这听起来像是一个都市传说.

                  This sounds like an urban legend to me.

                  bin2hex() 将输入中的每个字节映射到输出中的 两个 字节('a' -> '61'),因此您应该注意到执行查询的脚本显着增加了内存 - 它应该使用至少与要插入的二进制数据的字节长度一样多的内存.

                  bin2hex() maps each byte in the input to two bytes in the output ('a' -> '61'), so you should notice a significant memory increase of the script performing the query - it should use at least as much memory more as the byte length of the binary data to be inserted.

                  此外,这意味着在长字符串上运行 bin2hex() 比运行 mysql_real_escape string() 花费 much 更长的时间,即MySQL 的文档 中解释 - 只是转义6 个字符:NULL 和'Control-Z'.

                  Furthermore, this implies that running bin2hex() on a long string takes much longer than running mysql_real_escape string(), which - as explained in MySQL's documentation - just escapes 6 characters: NULL, , , , , and 'Control-Z'.

                  那是 PHP 部分,现在是 MySQL:服务器需要执行反向操作才能正确存储数据.反转任何一个函数所花费的时间几乎与原始操作一样长 - mysql_real_escape_string() 的反转函数需要将转义值 (\) 替换为非转义值 (),而 bin2hex() 的逆操作则需要将 每个字节元组 替换为一个新字节.

                  That was for the PHP part, now for MySQL: The server needs to do the reverse operation to store the data correctly. Reversing either of the functions takes almost as long as the original operation - the reverse function of mysql_real_escape_string() needs to replace escaped values (\) with unescaped ones (), whereas the reverse of bin2hex() would need to replace each and every byte tuple with a new byte.

                  由于在二进制数据上调用 mysql_real_escape_string() 是安全的(根据 MySQL 和 PHP 的文档 或者即使只是考虑到该操作除了上面列出的之外没有进行任何其他转换),执行如此昂贵的操作绝对没有意义.

                  Since calling mysql_real_escape_string() on binary data is safe (according to MySQL's and PHP's documentation or even when just considering that the operation does not do any other conversions than the ones listed above), it would make absolutely no sense to perform such a costly operation.

                  这篇关于为什么在将二进制数据从 PHP 插入 MySQL 时使用 bin2hex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  • <tfoot id='NmWSe'></tfoot>
                    • <bdo id='NmWSe'></bdo><ul id='NmWSe'></ul>

                      <small id='NmWSe'></small><noframes id='NmWSe'>

                          <tbody id='NmWSe'></tbody>

                      • <legend id='NmWSe'><style id='NmWSe'><dir id='NmWSe'><q id='NmWSe'></q></dir></style></legend>
                        1. <i id='NmWSe'><tr id='NmWSe'><dt id='NmWSe'><q id='NmWSe'><span id='NmWSe'><b id='NmWSe'><form id='NmWSe'><ins id='NmWSe'></ins><ul id='NmWSe'></ul><sub id='NmWSe'></sub></form><legend id='NmWSe'></legend><bdo id='NmWSe'><pre id='NmWSe'><center id='NmWSe'></center></pre></bdo></b><th id='NmWSe'></th></span></q></dt></tr></i><div id='NmWSe'><tfoot id='NmWSe'></tfoot><dl id='NmWSe'><fieldset id='NmWSe'></fieldset></dl></div>