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

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

      <legend id='Fyz0r'><style id='Fyz0r'><dir id='Fyz0r'><q id='Fyz0r'></q></dir></style></legend>
    1. <tfoot id='Fyz0r'></tfoot>
        <bdo id='Fyz0r'></bdo><ul id='Fyz0r'></ul>

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

      MySQLi:查詢 VS 準備

      MySQLi: query VS prepare(MySQLi:查詢 VS 準備)

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

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

        <tfoot id='fi1kj'></tfoot>

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

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

            <tbody id='fi1kj'></tbody>

              1. 本文介紹了MySQLi:查詢 VS 準備的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                mysqli 中的preparequery 有一點我不太明白.

                There is something I don't quite understand it at all which is prepare and query in mysqli.

                這個是使用mysqli::query來處理查詢的,眾所周知它缺乏安全性:

                This one is using mysqli::query to process the query and it has been known of lacking security:

                public function fetch_assoc($query)
                    {
                        $result = parent::query($query);
                        //$result = self::preparedStatement($query);
                        if($result) 
                        {
                            return $result->fetch_assoc();
                        } 
                        else
                        {
                            # call the get_error function
                            return self::get_error();
                            # or:
                            # return $this->get_error();
                        }
                    }
                

                這是我認為具有更好安全性的準備綁定執行的那個,

                this is the one with prepare-bind-execute which has a better security I assume,

                public function fetch_assoc_stmt($sql,$types = null,$params = null)
                    {
                        # create a prepared statement
                        $stmt = parent::prepare($sql);
                
                        # bind parameters for markers
                        # but this is not dynamic enough...
                        //$stmt->bind_param("s", $parameter);
                
                        if($types&&$params)
                        {
                            $bind_names[] = $types;
                            for ($i=0; $i<count($params);$i++) 
                            {
                                $bind_name = 'bind' . $i;
                                $$bind_name = $params[$i];
                                $bind_names[] = &$$bind_name;
                            }
                            $return = call_user_func_array(array($stmt,'bind_param'),$bind_names);
                        }
                
                        # execute query 
                        $stmt->execute();
                
                        # these lines of code below return one dimentional array, similar to mysqli::fetch_assoc()
                        $meta = $stmt->result_metadata(); 
                
                        while ($field = $meta->fetch_field()) { 
                            $var = $field->name; 
                            $$var = null; 
                            $parameters[$field->name] = &$$var; 
                        }
                
                        call_user_func_array(array($stmt, 'bind_result'), $parameters); 
                
                        while($stmt->fetch()) 
                        { 
                            return $parameters;  
                        }
                
                        # close statement
                        $stmt->close();
                    }
                

                然而,這兩種方法都返回相同的結果,

                However, both of these two methods return the same result,

                $mysqli = new database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
                
                $sql = "
                SELECT *
                FROM root_contacts_cfm
                ORDER BY cnt_id DESC
                ";
                print_r($mysqli->fetch_assoc_stmt($sql));
                
                print_r($mysqli->fetch_assoc($sql));
                

                他們打印:

                Array
                (
                    [cnt_id] => 2
                    [cnt_email1] => lau@xx.net
                    [cnt_email2] => 
                    [cnt_fullname] => Lau T
                    [cnt_firstname] => Thiam
                    [cnt_lastname] => Lau
                    [cnt_organisation] => 
                    [cnt_website] => 
                    [cnt_biography] => 
                    [cnt_gender] => 
                    [cnt_birthday] => 
                    [cnt_address] => 
                    [cnt_postcode] => 
                    [cnt_telephone] => 
                    [cnt_note] => 
                    [cnt_key] => 
                    [cat_id] => 
                    [tcc_id] => 
                    [cnt_suspended] => 0
                    [cnt_created] => 2011-02-04 00:00:00
                    [cnt_updated] => 2011-02-04 13:54:36
                )
                Array
                (
                    [cnt_id] => 2
                    [cnt_email1] => lau@xx.net
                    [cnt_email2] => 
                    [cnt_fullname] => Lau T
                    [cnt_firstname] => Thiam
                    [cnt_lastname] => Lau
                    [cnt_organisation] => 
                    [cnt_website] => 
                    [cnt_biography] => 
                    [cnt_gender] => 
                    [cnt_birthday] => 
                    [cnt_address] => 
                    [cnt_postcode] => 
                    [cnt_telephone] => 
                    [cnt_note] => 
                    [cnt_key] => 
                    [cat_id] => 
                    [tcc_id] => 
                    [cnt_suspended] => 0
                    [cnt_created] => 2011-02-04 00:00:00
                    [cnt_updated] => 2011-02-04 13:54:36
                )
                

                您應該已經注意到,在fetch_assoc_stmt 的方法中,我根本沒有使用fetch_assoc.可能根本沒有機會使用它,因為 prepare 使用不同的方式返回結果.

                You should have noticed that inside the method of fetch_assoc_stmt I don't use fetch_assoc at all. Probably no chance at all in using it as prepare uses a different way to return result.

                那么,我的問題是,既然使用preparequery 好,為什么要存在fetch_assoc?我們不應該忘記它還是不應該 php.net 棄用它?fetch_all 也是一樣的 - 為什么我們首先要擁有它!??

                So, my question is since using prepare is better than query, why should fetch_assoc exist at all? Shouldn't we just forget about it or shouldn't php.net have it deprecated?? It is the same for fetch_all - why should we have it in the first place!??

                謝謝.

                推薦答案

                當您使用參數動態生成查詢時,準備好的語句比普通 SQL 查詢更可取.在您的示例中,您的 SQL 不包含任何變量,因此使用普通查詢或準備好的語句在功能上是等效的.

                Prepared statements are preferable to plain SQL queries when you are using parameters to dynamically generate the query. In you example, your SQL contains no variables, so using a plain query or prepared statement are functionally equivalent.

                當你必須改變參數的值時,例如在 WHERE 子句中,準備好的語句會給你增加安全性:

                When you must change the values of parameters, in the WHERE clause, for example, then prepared statements will give you added security:

                ...
                WHERE col1 = ? AND col2 = ?
                

                但是當您的查詢簡單且固定時,使用 $mysqli->query($sql)fetch_assoc() 可能需要較少的代碼.使用直接查詢而不是準備好的語句并不是普遍的壞習慣,正如您可能相信的那樣.當您的查詢需要參數化時,或者當同一個查詢必須重復編譯和執行時,您將從準備好的語句中受益.

                But when your query is simple and fixed, it may require less code to use $mysqli->query($sql) along with fetch_assoc(). Using direct queries rather than prepared statements is not a universally bad practice, as some might have you believe. When your query requires parameterization, or when the same query must be compiled and executed repeatedly, then you'll benefit from the prepared statement.

                這篇關于MySQLi:查詢 VS 準備的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 中保持其類型?)

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

                      <tbody id='zmXWv'></tbody>
                  1. <tfoot id='zmXWv'></tfoot>

                      <bdo id='zmXWv'></bdo><ul id='zmXWv'></ul>
                    • <i id='zmXWv'><tr id='zmXWv'><dt id='zmXWv'><q id='zmXWv'><span id='zmXWv'><b id='zmXWv'><form id='zmXWv'><ins id='zmXWv'></ins><ul id='zmXWv'></ul><sub id='zmXWv'></sub></form><legend id='zmXWv'></legend><bdo id='zmXWv'><pre id='zmXWv'><center id='zmXWv'></center></pre></bdo></b><th id='zmXWv'></th></span></q></dt></tr></i><div class="w0u0yue" id='zmXWv'><tfoot id='zmXWv'></tfoot><dl id='zmXWv'><fieldset id='zmXWv'></fieldset></dl></div>
                      <legend id='zmXWv'><style id='zmXWv'><dir id='zmXWv'><q id='zmXWv'></q></dir></style></legend>
                          主站蜘蛛池模板: 北京公积金代办/租房发票/租房备案-北京金鼎源公积金提取服务中心 | 【德信自动化】点胶机_全自动点胶机_自动点胶机厂家_塑料热压机_自动螺丝机-深圳市德信自动化设备有限公司 | 烘箱-工业烘箱-工业电炉-实验室干燥箱 - 苏州华洁烘箱制造有限公司 | 乐考网-银行从业_基金从业资格考试_初级/中级会计报名时间_中级经济师 | 京港视通报道-质量走进大江南北-京港视通传媒[北京]有限公司 | 工业胀紧套_万向节联轴器_链条-规格齐全-型号选购-非标订做-厂家批发价格-上海乙谛精密机械有限公司 | 砂石生产线_石料生产线设备_制砂生产线设备价格_生产厂家-河南中誉鼎力智能装备有限公司 | 长沙广告公司|长沙广告制作设计|长沙led灯箱招牌制作找望城湖南锦蓝广告装饰工程有限公司 | 亿立分板机_曲线_锯片式_走刀_在线式全自动_铣刀_在线V槽分板机-杭州亿协智能装备有限公司 | 中式装修设计_全屋定制家具_实木仿古门窗花格厂家-喜迎门 | 硬齿面减速机[型号全],ZQ减速机-淄博久增机械 | 乐泰胶水_loctite_乐泰胶_汉高乐泰授权(中国)总代理-鑫华良供应链 | 粉碎机_塑料粉碎机_塑料破碎机厂家-星标机械 | 高精度-恒温冷水机-螺杆式冰水机-蒸发冷冷水机-北京蓝海神骏科技有限公司 | TPM咨询,精益生产管理,5S,6S现场管理培训_华谋咨询公司 | 耐高温电缆厂家-远洋高温电缆| 防渗土工膜|污水处理防渗膜|垃圾填埋场防渗膜-泰安佳路通工程材料有限公司 | 远程会诊系统-手术示教系统【林之硕】医院远程医疗平台 | 高精度电阻回路测试仪-回路直流电阻测试仪-武汉特高压电力科技有限公司 | 橡胶膜片,夹布膜片,橡胶隔膜密封,泵阀设备密封膜片-衡水汉丰橡塑科技公司网站 | 紧急切断阀_气动切断阀_不锈钢阀门_截止阀_球阀_蝶阀_闸阀-上海上兆阀门制造有限公司 | 防爆电机_防爆电机型号_河南省南洋防爆电机有限公司 | 时代北利离心机,实验室离心机,医用离心机,低速离心机DT5-2,美国SKC采样泵-上海京工实业有限公司 工业电炉,台车式电炉_厂家-淄博申华工业电炉有限公司 | 外贸资讯网 - 洞悉全球贸易,把握市场先机 | 智能门锁电机_智能门锁离合器_智能门锁电机厂家-温州劲力智能科技有限公司 | 科客,主见不成见| 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | 衡阳耐适防护科技有限公司——威仕盾焊接防护用品官网/焊工手套/焊接防护服/皮革防护手套 | 细砂提取机,隔膜板框泥浆污泥压滤机,螺旋洗砂机设备,轮式洗砂机械,机制砂,圆锥颚式反击式破碎机,振动筛,滚筒筛,喂料机- 上海重睿环保设备有限公司 | 发电机组|柴油发电机组-批发,上柴,玉柴,潍柴,康明斯柴油发电机厂家直销 | 诺冠气动元件,诺冠电磁阀,海隆防爆阀,norgren气缸-山东锦隆自动化科技有限公司 | 翰墨AI智能写作助手官网_人工智能问答在线AI写作免费一键生成 | 橡胶电子拉力机-塑料-微电脑电子拉力试验机厂家-江苏天源 | 纯化水设备-纯水设备-超纯水设备-[大鹏水处理]纯水设备一站式服务商-东莞市大鹏水处理科技有限公司 | DWS物流设备_扫码称重量方一体机_快递包裹分拣机_广东高臻智能装备有限公司 | 便携式XPDM露点仪-在线式防爆露点仪-增强型烟气分析仪-约克仪器 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 | 中国玩具展_玩具展|幼教用品展|幼教展|幼教装备展 | 伺服电机_直流伺服_交流伺服_DD马达_拓达官方网站 | 德州万泰装饰 - 万泰装饰装修设计软装家居馆 | 江苏皓越真空设备有限公司 |