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

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

    1. <legend id='P8Fyq'><style id='P8Fyq'><dir id='P8Fyq'><q id='P8Fyq'></q></dir></style></legend>
    2. <tfoot id='P8Fyq'></tfoot>
      • <bdo id='P8Fyq'></bdo><ul id='P8Fyq'></ul>

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

      如何訪問另一個頁面上另一個類中的mysqli連接?

      How to access mysqli connection in another class on another page?(如何訪問另一個頁面上另一個類中的mysqli連接?)

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

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

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

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

                  <tbody id='yxbC3'></tbody>
                本文介紹了如何訪問另一個頁面上另一個類中的mysqli連接?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                如何在用戶類中打開數據庫連接,在那里進行數據庫操作?以及為什么需要在 DBConnection 類中定義內置的創建函數..????

                我已經創建了

                1. db.php
                2. user.php
                3. result.php

                在 db.php

                class DBConnection{受保護的 $mysqli;私人 $db_host="127.0.0.1";私人 $db_name="測試";私人 $db_username="root";私人 $db_password="";公共函數 __construct(){$this->mysqli=new mysqli($this->db_host,$this->db_username,$this->db_password,$this->db_name) 或 die($this->mysqli->error);返回 $this->mysqli;}public function query($query)//為什么我需要創建這個函數{返回 $this->mysqli->query($query);}公共函數 real_escape_string($str){返回 $this->mysqli->real_escape_string();}函數 __destruct(){//關閉連接$this->mysqli->close();}}?>

                在 User.php

                mysqli=$conn;}公共函數 addUser($name,$username,$email,$pwd){$query="";$res=$this->mysqli->query($query);//請查一下DBConnection中的查詢函數,需要定義什么查詢>DBConnection 中的函數?返回 $res;}}?>

                在 result.php

                 

                <塊引用>

                 $name = $conn->real_escape_string(trim(strip_tags($_POST['name'])));$username = $conn->real_escape_string(trim(strip_tags($_POST['username'])));$email = $conn->real_escape_string(trim(strip_tags($_POST['email'])));$password = $conn->real_escape_string(trim(strip_tags($_POST['pass'])));

                //echo $name."".$username."".$email."".$password;$uObj=新用戶($conn);$uObj->addUser($name,$username,$email,$password);echo "你好,你的名字是<I>".$name."</I>并且你的電子郵件ID是<I>".$email."</I>";}?>

                解決方案

                你的 DBConnection 類需要一個額外的方法:

                公共函數getLink(){返回 $this->mysqli;}

                看來您原來的 User 類是 DBConnection 的子類,因為 DBConnection 上的 mysqli 屬性是protectedUser 類有一個 parent::__construct() 調用.

                最好使用依賴注入,這樣您的 User 類將通過構造函數接收其數據庫連接:

                公共函數__construct(DBConnection $db){$this->mysqli = $db->getLink();}

                然后你可以從你的代碼運行:

                $db = 新的 DBConnection;$uObj = 新用戶($db);

                How can I open a database connection in user class , where I can do database operation? and why need to define inbuilt created functions in DBConnection class ..????

                I have created

                1. db.php
                2. user.php
                3. result.php

                in db.php

                class DBConnection  
                {       
                  protected $mysqli;
                  private  $db_host="127.0.0.1";
                  private  $db_name="test";
                  private  $db_username="root";
                  private  $db_password="";
                
                  public function __construct()
                    {
                        $this->mysqli=new mysqli($this->db_host,$this->db_username,
                                $this->  db_password,$this->db_name) or die($this->mysqli->error);
                
                         return $this->mysqli;
                    }
                
                public function query($query)  // why i need to creat this function
                     {
                    return $this->mysqli->query($query);
                     }
                
                public function real_escape_string($str)
                 {
                    return $this->mysqli->real_escape_string();
                 }
                
                
                   function __destruct(){
                     //Close the Connection
                     $this->mysqli->close();
                    }
                }
                ?>
                

                in User.php

                <?php
                  require "db.php"; 
                
                  class User {  
                
                  public function __construct($conn)
                {
                    $this->mysqli=$conn;
                
                }
                
                   public function addUser($name,$username,$email,$pwd)
                   {
                     $query="  ";
                
                     $res=$this->mysqli->query($query);
                
                  //pls chek the query function in DBConnection,what is the need to define query               >       function in DBConnection ?
                
                    return $res;
                    }
                   }    
                ?>
                

                in result.php

                    <?php
                
                      require "user.php";
                
                      $conn=new DBConnection();
                
                      if(isset($_POST['submit']))
                  {
                

                 $name = $conn->real_escape_string(trim(strip_tags($_POST['name'])));
                   $username =  $conn->real_escape_string(trim(strip_tags($_POST['username'])));
                   $email =     $conn->real_escape_string(trim(strip_tags($_POST['email'])));
                   $password    = $conn->real_escape_string(trim(strip_tags($_POST['pass'])));
                

                //echo $name."".$username."".$email."".$password;
                  $uObj=new User($conn);
                  $uObj->addUser($name,$username,$email,$password);
                
                
                echo " hi your name is <I>".$name."</I> and your email ID is <I>".$email."</I>";
                        }
                
                
                ?>
                

                解決方案

                Your DBConnection class would need an additional method:

                public function getLink()
                {
                    return $this->mysqli;
                }
                

                It seems that your original User class was a subclass of DBConnection, because mysqli property on DBConnection is protected and User class has a parent::__construct() call.

                It's better to use dependency injection, so your User class will receive its database connection via the constructor:

                public function __construct(DBConnection $db)
                {
                    $this->mysqli = $db->getLink();
                }
                

                Then from your code you can run:

                $db = new DBConnection;
                $uObj = new User($db);
                

                這篇關于如何訪問另一個頁面上另一個類中的mysqli連接?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 中保持其類型?)
                      1. <legend id='I05J3'><style id='I05J3'><dir id='I05J3'><q id='I05J3'></q></dir></style></legend>
                      2. <i id='I05J3'><tr id='I05J3'><dt id='I05J3'><q id='I05J3'><span id='I05J3'><b id='I05J3'><form id='I05J3'><ins id='I05J3'></ins><ul id='I05J3'></ul><sub id='I05J3'></sub></form><legend id='I05J3'></legend><bdo id='I05J3'><pre id='I05J3'><center id='I05J3'></center></pre></bdo></b><th id='I05J3'></th></span></q></dt></tr></i><div class="mk20e2k" id='I05J3'><tfoot id='I05J3'></tfoot><dl id='I05J3'><fieldset id='I05J3'></fieldset></dl></div>
                          <bdo id='I05J3'></bdo><ul id='I05J3'></ul>
                            <tbody id='I05J3'></tbody>
                        • <tfoot id='I05J3'></tfoot>

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

                          主站蜘蛛池模板: 超声波清洗机-超声波清洗设备定制生产厂家 - 深圳市冠博科技实业有限公司 | 水稻烘干机,小麦烘干机,大豆烘干机,玉米烘干机,粮食烘干机_巩义市锦华粮食烘干机械制造有限公司 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 齿式联轴器-弹性联轴器-联轴器厂家-江苏诺兴传动联轴器制造有限公司 | 恒温恒湿试验箱厂家-高低温试验箱维修价格_东莞环仪仪器_东莞环仪仪器 | 北京网络营销推广_百度SEO搜索引擎优化公司_网站排名优化_谷歌SEO - 北京卓立海创信息技术有限公司 | 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 | 硅PU球场、篮球场地面施工「水性、环保、弹性」硅PU材料生产厂家-广东中星体育公司 | 列管冷凝器,刮板蒸发器,外盘管反应釜厂家-无锡曼旺化工设备有限公司 | 爱佩恒温恒湿测试箱|高低温实验箱|高低温冲击试验箱|冷热冲击试验箱-您身边的模拟环境试验设备技术专家-合作热线:400-6727-800-广东爱佩试验设备有限公司 | Pos机办理_个人商户免费POS机申请-拉卡拉办理网 | 淘气堡_室内儿童乐园_户外无动力儿童游乐设备-高乐迪(北京) | 棕刚玉_白刚玉_铝酸钙-锐石新材料| 量子管通环-自清洗过滤器-全自动反冲洗过滤器-北京罗伦过滤技术集团有限公司 | 聚合甘油__盐城市飞龙油脂有限公司 | 深圳市东信高科自动化设备有限公司 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 杭州公司变更法人-代理记账收费价格-公司注销代办_杭州福道财务管理咨询有限公司 | 食品无尘净化车间,食品罐装净化车间,净化车间配套风淋室-青岛旭恒洁净技术有限公司 | 定硫仪,量热仪,工业分析仪,马弗炉,煤炭化验设备厂家,煤质化验仪器,焦炭化验设备鹤壁大德煤质工业分析仪,氟氯测定仪 | 润东方环保空调,冷风机,厂房车间降温设备-20年深圳环保空调生产厂家 | 冷镦机-多工位冷镦机-高速冷镦机厂家-温州金诺机械设备制造有限公司 | 山东成考网-山东成人高考网 | 潍坊大集网-潍坊信息港-潍坊信息网 | 上海皓越真空设备有限公司官网-真空炉-真空热压烧结炉-sps放电等离子烧结炉 | 水稻烘干机,小麦烘干机,大豆烘干机,玉米烘干机,粮食烘干机_巩义市锦华粮食烘干机械制造有限公司 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 油缸定制-液压油缸厂家-无锡大鸿液压气动成套有限公司 | 焊缝跟踪系统_激光位移传感器_激光焊缝跟踪传感器-创想智控 | 厦门网站建设_厦门网站设计_小程序开发_网站制作公司【麦格科技】 | 脑钠肽-白介素4|白介素8试剂盒-研域(上海)化学试剂有限公司 | 湖州织里童装_女童男童中大童装_款式多尺码全_织里儿童网【官网】-嘉兴嘉乐网络科技有限公司 | 双舌接地线-PC68数字式高阻计-ZC36|苏海百科 | 重庆网站建设,重庆网站设计,重庆网站制作,重庆seo,重庆做网站,重庆seo,重庆公众号运营,重庆小程序开发 | 高效复合碳源-多核碳源生产厂家-污水处理反硝化菌种一长隆科技库巴鲁 | 无锡门窗-系统门窗-阳光房-封阳台-断桥铝门窗厂[窗致美] | 避光流动池-带盖荧光比色皿-生化流动比色皿-宜兴市晶科光学仪器 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | TPE_TPE热塑性弹性体_TPE原料价格_TPE材料厂家-惠州市中塑王塑胶制品公司- 中塑王塑胶制品有限公司 | 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 酒店厨房设计_中央厨房设计_北京商用厨房设计公司-奇能商厨 | 成人纸尿裤,成人尿不湿,成人护理垫-山东康舜日用品有限公司 | 珠海冷却塔降噪维修_冷却塔改造报价_凉水塔风机维修厂家- 广东康明节能空调有限公司 | 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 |