如何访问另一个页面上另一个类中的mysqli连接?

How to access mysqli connection in another class on another page?(如何访问另一个页面上另一个类中的mysqli连接?)
本文介绍了如何访问另一个页面上另一个类中的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连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

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 的问题)