pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <legend id='yLh4N'><style id='yLh4N'><dir id='yLh4N'><q id='yLh4N'></q></dir></style></legend>

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

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

    1. <tfoot id='yLh4N'></tfoot>
          <bdo id='yLh4N'></bdo><ul id='yLh4N'></ul>

      1. 如何使用 mysqli API 制作一個完全動態的準備語句

        How to make a fully dynamic prepared statement using mysqli API?(如何使用 mysqli API 制作一個完全動態的準備語句?)
      2. <legend id='r5YSr'><style id='r5YSr'><dir id='r5YSr'><q id='r5YSr'></q></dir></style></legend>
        <i id='r5YSr'><tr id='r5YSr'><dt id='r5YSr'><q id='r5YSr'><span id='r5YSr'><b id='r5YSr'><form id='r5YSr'><ins id='r5YSr'></ins><ul id='r5YSr'></ul><sub id='r5YSr'></sub></form><legend id='r5YSr'></legend><bdo id='r5YSr'><pre id='r5YSr'><center id='r5YSr'></center></pre></bdo></b><th id='r5YSr'></th></span></q></dt></tr></i><div class="tidgxzn" id='r5YSr'><tfoot id='r5YSr'></tfoot><dl id='r5YSr'><fieldset id='r5YSr'></fieldset></dl></div>

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

        1. <tfoot id='r5YSr'></tfoot>

                <tbody id='r5YSr'></tbody>
                • <bdo id='r5YSr'></bdo><ul id='r5YSr'></ul>

                • 本文介紹了如何使用 mysqli API 制作一個完全動態的準備語句?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我需要更改此查詢以使用準備好的語句.可能嗎?

                  I need to change this query to use a prepared statement. Is it possible?

                  查詢:

                  $sql = "SELECT id, title, content, priority, date, delivery FROM tasks " . $op . " " . $title . " " . $content . " " . $priority . " " . $date . " " . $delivery . " ORDER BY " . $orderField . " " . $order . " " . $pagination . "";
                  

                  在查詢之前,有代碼來檢查 POST 變量并更改查詢中變量的內容.

                  Before the query, there's code to check the POST variables and change the content of variables in the query.

                  //For $op makes an INNER JOIN with or without IN clause depending on the content of a $_POST variable
                  $op = "INNER JOIN ... WHERE opID  IN ('"$.opID."')";
                  //Or
                  $op = "INNER JOIN ... ";
                  
                  //For $title (depends of $op):
                  $title = "WHERE title LIKE'%".$_POST["title"]."%'";
                  //Or
                  $title = "AND title LIKE'%".$_POST["title"]."%'";
                  
                  //For $content:
                  $content = "AND content LIKE '%".$_POST["content"]."%'";
                  
                  //For $priority just a switch:
                  $priority = "AND priority = DEPENDING_CASE";
                  
                  //For $date and $delivery another switch 
                  $d = date("Y-m-d", strtotime($_POST["date"]));
                  $date = "AND date >= '$d' 00:00:00 AND date <= '$d' 23:59:59";
                  //Or $date = "AND date >= '$d' 00:00:00";
                  //Or $date = "AND date <= '$d' 23:59:59";
                  
                  //For $orderField
                  $orderField = $_POST["column"];
                  
                  //For $order
                  $order= $_POST["order"];
                  
                  //For $pagination 
                  $pagination = "LIMIT ".$offset.",". $recordsPerPage;
                  

                  如何使用準備好的語句執行此查詢?

                  How I could do this query using prepared statement?

                  • 查詢可能更加靜態,但這意味著生成不同的準備好的語句并根據 $_POST 檢查執行它.
                  • 這取決于許多變量,因為此查詢在包含搜索字段和要排序的列的表中顯示結果.

                  一個完整的查詢示例如下(取決于 $_POST 檢查):

                  A full example of query would be like this (depending of $_POST checks):

                  SELECT id, title, content, priority, date, delivery FROM tasks INNER JOIN op ON task.op = op.opId WHERE op IN (4851,8965,78562) AND title LIKE '%PHT%' AND content LIKE '%%' AND priority = '2' ORDER BY date DESC LIMIT 0, 10 
                  

                  推薦答案

                  一個很好的問題.感謝您轉向準備好的聲明.似乎經過這么多年的斗爭,這個想法終于開始接管了.

                  An excellent question. And thank you for moving to prepared statements. It seems that after all those years of struggle, the idea finally is starting to take over.

                  免責聲明:會有指向我自己網站的鏈接,因為我已經幫助人們使用 PHP 20 多年,并且癡迷于撰寫有關最常見問題的文章.

                  是的,這完全有可能.查看我的文章,如何為 mysqli 創建搜索過濾器以獲得完整功能的示例.

                  Yes, it's perfectly possible. Check out my article, How to create a search filter for mysqli for the fully functional example.

                  對于 WHERE 部分,您只需要創建兩個單獨的數組 - 一個包含帶有占位符的查詢條件,另一個包含這些占位符的實際值,即:

                  For the WHERE part, all you need is to create two separate arrays - one containing query conditions with placeholders and one containing actual values for these placeholders, i.e:

                  $conditions = [];
                  $parameters = [];
                  
                  if (!empty($_POST["content"])) {
                      $conditions[] = 'content LIKE ?';
                      $parameters[] = '%'.$_POST['content ']."%";
                  }
                  

                  等,適用于所有搜索條件.

                  and so on, for all search conditions.

                  然后你可以implode使用AND字符串作為膠水的所有條件,并得到一個一流的WHERE子句:

                  Then you could implode all the conditions using AND string as a glue, and get a first-class WHERE clause:

                  if ($conditions)
                  {
                      $where .= " WHERE ".implode(" AND ", $conditions);
                  }
                  

                  所有搜索條件的例程都相同,但對于 IN() 子句會有所不同.

                  The routine is the same for all search conditions, but it will be a bit different for the IN() clause.

                  有點不同,因為您需要添加更多占位符和更多值:

                  is a bit different as you will need more placeholders and more values to be added:

                  if (!empty($_POST["opID"])) {
                      $in  = str_repeat('?,', count($array) - 1) . '?';
                      $conditions[] = "opID IN ($in)";
                      $parameters = array_merge($parameters, $_POST["opID"]);
                  }
                  

                  此代碼將向 IN() 子句添加與 $_POST["opID"] 中的元素一樣多的 ? 占位符并將所有這些值添加到 $parameters 數組中.解釋可以在我網站上同一部分的相鄰文章中找到.

                  this code will add as many ? placeholders to the IN() clause as many elements in the $_POST["opID"] and will add all those values to the $parameters array. The explanation can be found in the adjacent article in the same section on my site.

                  完成WHERE 子句后,您可以轉到查詢的其余部分

                  After you are done with WHERE clause, you can move to the rest of your query

                  您不能參數化 order by 子句,因為字段名稱和 SQL 關鍵字不能由占位符表示.為了解決這個問題,我懇求您使用我為此專門編寫的白名單功能.有了它,您可以使您的 ORDER BY 子句 100% 安全但非常靈活.您只需要預先定義一個數組,其中的字段名稱允許在 order by 子句中:

                  You cannot parameterize the order by clause, because field names and SQL keywords cannot be represented by a placeholder. And to tackle with this problem I beg you to use a whitelisting function I wrote for this exact purpose. With it you can make your ORDER BY clause 100% safe but perfectly flexible. All you need is to predefine an array with field names allowed in the order by clause:

                  $sortColumns = ["title","content","priority"]; // add your own
                  

                  然后使用這個方便的函數獲取安全值:

                  and then get safe values using this handy function:

                  $orderField = white_list($_POST["column"], $sortColumns, "Invalid column name");
                  $order = white_list($_POST["order"], ["ASC","DESC"], "Invalid ORDER BY direction");
                  

                  這是一個智能功能,涵蓋三種不同的場景

                  this is a smart function, that covers three different scenarios

                  • 如果沒有提供任何值(即 $_POST["column"] 為空),將使用白名單中的第一個值,因此它用作默認值
                  • 如果提供了正確的值,它將在查詢中使用
                  • 如果提供的值不正確,則會引發錯誤.

                  LIMIT 值是完全參數化的,因此您可以將它們添加到 $parameters 數組中:

                  LIMIT values are perfectly parameterized so you can just add them to the $parameters array:

                  $limit = "LIMIT ?, ?";
                  $parameters[] = $offset;
                  $parameters[] = $recordsPerPage;
                  

                  總裝

                  最后,您的查詢將是這樣的

                  The final assembly

                  In the end, your query will be something like this

                  $sql = "SELECT id, title, content, priority, date, delivery 
                          FROM tasks INNER JOIN ... $where ORDER BY `$orderField` $order $limit"; 
                  

                  并且可以使用以下代碼執行

                  And it can be executed using the following code

                  $stmt = $mysqli->prepare($sql);
                  $stmt->bind_param(str_repeat("s", count($parameters)), ...$parameters);
                  $stmt->execute();
                  $data = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
                  

                  其中 $data 是一個包含查詢返回的所有行的常規數組.

                  where $data is a conventional array contains all the rows returned by the query.

                  這篇關于如何使用 mysqli API 制作一個完全動態的準備語句?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  store_result() and get_result() in mysql returns false(mysql 中的 store_result() 和 get_result() 返回 false)
                  Call to undefined function mysqli_result::num_rows()(調用未定義的函數 mysqli_result::num_rows())
                  PHP Prepared Statement Problems(PHP 準備好的語句問題)
                  mysqli_fetch_array returning only one result(mysqli_fetch_array 只返回一個結果)
                  PHP MySQLi Multiple Inserts(PHP MySQLi 多次插入)
                  How do I make sure that values from MySQL keep their type in PHP?(如何確保 MySQL 中的值在 PHP 中保持其類型?)

                    <bdo id='Ti3iU'></bdo><ul id='Ti3iU'></ul>

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

                    <i id='Ti3iU'><tr id='Ti3iU'><dt id='Ti3iU'><q id='Ti3iU'><span id='Ti3iU'><b id='Ti3iU'><form id='Ti3iU'><ins id='Ti3iU'></ins><ul id='Ti3iU'></ul><sub id='Ti3iU'></sub></form><legend id='Ti3iU'></legend><bdo id='Ti3iU'><pre id='Ti3iU'><center id='Ti3iU'></center></pre></bdo></b><th id='Ti3iU'></th></span></q></dt></tr></i><div class="3zgbku2" id='Ti3iU'><tfoot id='Ti3iU'></tfoot><dl id='Ti3iU'><fieldset id='Ti3iU'></fieldset></dl></div>
                    <legend id='Ti3iU'><style id='Ti3iU'><dir id='Ti3iU'><q id='Ti3iU'></q></dir></style></legend>

                          <tfoot id='Ti3iU'></tfoot>
                              <tbody id='Ti3iU'></tbody>
                          • 主站蜘蛛池模板: 合肥展厅设计-安徽展台设计-合肥展览公司-安徽奥美展览工程有限公司 | 体坛网_体坛+_体坛周报新闻客户端 | 视频直播 -摄影摄像-视频拍摄-直播分发 | 南京展台搭建-南京展会设计-南京展览设计公司-南京展厅展示设计-南京汇雅展览工程有限公司 | 酒精检测棒,数显温湿度计,酒安酒精测试仪,酒精检测仪,呼气式酒精检测仪-郑州欧诺仪器有限公司 | 山东石英砂过滤器,除氟过滤器「价格低」-淄博胜达水处理 | 123悬赏网_发布悬赏任务_广告任务平台 | 厦门ISO认证|厦门ISO9001认证|厦门ISO14001认证|厦门ISO45001认证-艾索咨询专注ISO认证行业 | 外贮压-柜式-悬挂式-七氟丙烷-灭火器-灭火系统-药剂-价格-厂家-IG541-混合气体-贮压-非贮压-超细干粉-自动-灭火装置-气体灭火设备-探火管灭火厂家-东莞汇建消防科技有限公司 | 干式变压器厂_干式变压器厂家_scb11/scb13/scb10/scb14/scb18干式变压器生产厂家-山东科锐变压器有限公司 | 对辊式破碎机-对辊制砂机-双辊-双齿辊破碎机-巩义市裕顺机械制造有限公司 | 小型气象站_车载气象站_便携气象站-山东风途物联网 | 高楼航空障碍灯厂家哪家好_航空障碍灯厂家_广州北斗星障碍灯有限公司 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 热镀锌槽钢|角钢|工字钢|圆钢|H型钢|扁钢|花纹板-天津千百顺钢铁贸易有限公司 | 一体化污水处理设备_生活污水处理设备_全自动加药装置厂家-明基环保 | 食药成分检测_调料配方还原_洗涤剂化学成分分析_饲料_百检信息科技有限公司 | 团建-拓展-拓展培训-拓展训练-户外拓展训练基地[无锡劲途] | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | Akribis直线电机_直线模组_力矩电机_直线电机平台|雅科贝思Akribis-杭州摩森机电科技有限公司 | 破碎机锤头_合金耐磨锤头_郑州宇耐机械工程技术有限公司 | 退火炉,燃气退火炉,燃气热处理炉生产厂家-丹阳市丰泰工业炉有限公司 | KBX-220倾斜开关|KBW-220P/L跑偏开关|拉绳开关|DHJY-I隔爆打滑开关|溜槽堵塞开关|欠速开关|声光报警器-山东卓信有限公司 | 潍坊大集网-潍坊信息港-潍坊信息网 | 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | 急救箱-应急箱-急救包厂家-北京红立方医疗设备有限公司 | 家用净水器代理批发加盟_净水机招商代理_全屋净水器定制品牌_【劳伦斯官网】 | 酒瓶_酒杯_玻璃瓶生产厂家_徐州明政玻璃制品有限公司 | 昆山新莱洁净应用材料股份有限公司-卫生级蝶阀,无菌取样阀,不锈钢隔膜阀,换向阀,离心泵 | 聚氨酯复合板保温板厂家_廊坊华宇创新科技有限公司 | 金属检测机_金属分离器_检针验针机_食品药品金属检探测仪器-广东善安科技 | 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 偏心半球阀-电动偏心半球阀-调流调压阀-旋球阀-上欧阀门有限公司 | 开锐教育-学历提升-职称评定-职业资格培训-积分入户 | 膜结构车棚|上海膜结构车棚|上海车棚厂家|上海膜结构公司 | 电池挤压试验机-自行车喷淋-车辆碾压试验装置-深圳德迈盛测控设备有限公司 | 山东氧化铁红,山东铁红-淄博科瑞化工有限公司 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 岩石钻裂机-液压凿岩机-劈裂机-挖改钻_湖南烈岩科技有限公司 | 节流截止放空阀-不锈钢阀门-气动|电动截止阀-鸿华阀门有限公司 | 药品/药物稳定性试验考察箱-埃里森仪器设备(上海)有限公司 |