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

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

        <tfoot id='s6BY7'></tfoot>

      1. <legend id='s6BY7'><style id='s6BY7'><dir id='s6BY7'><q id='s6BY7'></q></dir></style></legend>

          <bdo id='s6BY7'></bdo><ul id='s6BY7'></ul>
      2. PHP:使用 PDO 从 MySQL 检索图像

        时间:2023-09-20
          <tbody id='RNzDV'></tbody>

        <legend id='RNzDV'><style id='RNzDV'><dir id='RNzDV'><q id='RNzDV'></q></dir></style></legend><tfoot id='RNzDV'></tfoot>

            <bdo id='RNzDV'></bdo><ul id='RNzDV'></ul>
          • <small id='RNzDV'></small><noframes id='RNzDV'>

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

                • 本文介绍了PHP:使用 PDO 从 MySQL 检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在重构一些旧代码,包括重写基本的 mysql 查询以使用 PDO.

                  I am refactoring some old code, including rewriting basic mysql queries to use PDO.

                  以下适用于所有浏览器和所有图像类型:

                  The following works brilliantly in all browsers and for all image types:

                  $query = 'SELECT image FROM image WHERE imageid=' . $image_id;
                  $result = mysql_query($query, $db_conn); querycheck($result);
                  header("Content-type: image");
                  echo mysql_result($result, 0);
                  

                  不幸的是,尽管我使用 PDO 重写了它,但它不起作用.我已经浏览了整个 PDO 文档和标准的网络搜索,但没有任何建议/解决方案有效.

                  Unfortunately, however I rewrite it using PDO, it doesn't work. I've been through the entire PDO documentation and the standard web search, but none of the advice/solutions work.

                  如何使用 PDO 从 MySQL 中轻松获取图像并显示它?

                  How can one easily fetch and image from MySQL using PDO and display it?

                  编辑 1:

                  Matthew Ratzloff 给出了下面应该是显而易见的答案,但它不起作用.这是我使用 PDO 测试的实际代码(我尝试了许多变体/参数):

                  Matthew Ratzloff gives what should be the obvious answer below, but it does not work. Here is the actual code that I test using PDO (and I have tried many variants/parameters):

                  $connectstring_temp = 'mysql:host=' . $A . ';dbname=' .$B;
                  $dbh_temp = new PDO($connectstring_temp, $login, $password);
                  #$dbh_temp->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                  #$dbh_temp->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
                  
                  $sql = "SELECT image FROM image WHERE imageid=" . $image_id;
                  $query = $dbh_temp->prepare($sql);
                  $query->execute();
                  
                  $query->bindColumn(1, $image, PDO::PARAM_LOB);
                  $query->fetch(PDO::FETCH_BOUND);
                  header("Content-Type: image");
                  echo $image;
                  

                  我保留了相同的语法,但最终代码 $image_id 需要作为参数传递.上面的代码不起作用.PDO 适用于所有类型的所有其他查询.

                  I've kept the same syntax, although for the final code $image_id needs to be passed as a parameter. The code above does NOT work. PDO works fine for all other queries of all types.

                  推荐答案

                  需要参数化imageid值并将参数绑定到PDO::PARAM_LOB:

                  You need to paramaterize the imageid value and bind the parameter to PDO::PARAM_LOB:

                  $sql = "SELECT image FROM image WHERE imageid=:id";
                  $query = $db_conn->prepare($sql);
                  $query->execute(array(':id' => $image_id));
                  
                  $query->bindColumn(1, $image, PDO::PARAM_LOB);
                  $query->fetch(PDO::FETCH_BOUND);
                  header("Content-Type: image");
                  echo $image;
                  

                  当然,您还需要指定完整、正确的内容类型(例如,图像/png).

                  Of course, you'll also want to specify the complete, correct content type (e.g., image/png).

                  这篇关于PHP:使用 PDO 从 MySQL 检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用pdo从sql中删除数据? 下一篇:使用 PDO 从 MySQL 获取结果

                  相关文章

                  最新文章

                  • <bdo id='JSkFu'></bdo><ul id='JSkFu'></ul>

                    <legend id='JSkFu'><style id='JSkFu'><dir id='JSkFu'><q id='JSkFu'></q></dir></style></legend>

                    <tfoot id='JSkFu'></tfoot>
                    1. <small id='JSkFu'></small><noframes id='JSkFu'>

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