Php 如何从其他类调用我的数据库类

Php 如何从其他类调用我的数据库类,php,mysql,pdo,Php,Mysql,Pdo,我有一个数据库类,它成功地连接到我的本地主机和数据库,我想选择我的用户数据库中的所有用户,但是当我调用我的数据库时,事实证明这比我想象的要困难得多 我遇到了很多错误,例如: 注意:未定义变量:第10行E:\xampp\htdocs\attention\class.Register.php中的主机 注意:未定义变量:第10行E:\xampp\htdocs\attention\class.Register.php中的dbname 注意:未定义变量:第10行E:\xampp\htdocs\attent

我有一个数据库类,它成功地连接到我的本地主机和数据库,我想选择我的用户数据库中的所有用户,但是当我调用我的数据库时,事实证明这比我想象的要困难得多

我遇到了很多错误,例如:

注意:未定义变量:第10行E:\xampp\htdocs\attention\class.Register.php中的主机

注意:未定义变量:第10行E:\xampp\htdocs\attention\class.Register.php中的dbname

注意:未定义变量:第10行E:\xampp\htdocs\attention\class.Register.php中的用户

注意:未定义变量:在第10行传入E:\xampp\htdocs\attention\class.Register.php

致命错误:在第17行的E:\xampp\htdocs\attention\class.Register.php中调用未定义的方法Database::prepare()

然而,我已经在我的数据库类中定义了这些,并告诉我的注册类需要连接文件。我做错了什么?我似乎不知道应该如何在注册类中调用我的数据库连接?有人有什么想法吗

class.Connect.php

<?php

// Database connection PDO

class Database {

  public function __construct() {

  // Connection information
  $host   = 'localhost';
  $dbname = 'imanage';
  $user   = 'root';
  $pass   = '';

  // Attempt DB connection
  try
  {
    $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //echo 'Successfully connected to the database!';
   }
   catch(PDOException $e)
   {
    echo $e->getMessage();
   }        
  }

  public function __destruct()
  {
    // Disconnect from DB
    $this->pdo = null;
    //echo 'Successfully disconnected from the database!';
  }
}
?>
<?php

// Database connection PDO

class Database {

    public function __construct($host, $dbname, $user, $pass) {
        // Attempt DB connection
        try
        {
            $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

class.Register.php

<?php

require 'class.Connect.php';
class Register {

    public function __construct()
    {
           $this->pdo = new Database($host, $dbname, $user, $pass); //ofcourse you can get the db connections details and database name from a config file
    }

        public function viewall() {

    $sql = "SELECT * FROM users";
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute();
    // here you go:
    $users = $stmt->fetchAll();

    foreach ($users as $row) {
        print $row["firstname"] . "-" . $row["lastname"] ."<br/>";
    }
    }
}
$run = new Register();
$run->viewall();
?>
<?php

require 'class.Connect.php';

class Register {
        private $pdo;   // This variable will only be accessible from inside this class
    public function __construct($database)
         {

           $this->pdo = $database; //ofcourse you can get the db connections details and database name from a config file

您正在使用
$this->pdo
定义数据库连接,但忘记为类定义属性

定义类性能如下

class Register
{
    private  $pdo;
    public function __construct()
    {
         $this->pdo = new Database();    
    }

您的
数据库
类构造函数不接受任何参数,因此在创建
数据库
类的对象时避免传递参数。

您正在使用
$this->pdo
定义数据库连接,但忘记定义类的属性

定义类性能如下

class Register
{
    private  $pdo;
    public function __construct()
    {
         $this->pdo = new Database();    
    }

您的
数据库
类构造函数不接受任何参数,因此在创建
数据库
类的对象时,请避免传递参数。

我建议您按如下方式重新编写类

class.Connect.php

<?php

// Database connection PDO

class Database {

  public function __construct() {

  // Connection information
  $host   = 'localhost';
  $dbname = 'imanage';
  $user   = 'root';
  $pass   = '';

  // Attempt DB connection
  try
  {
    $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //echo 'Successfully connected to the database!';
   }
   catch(PDOException $e)
   {
    echo $e->getMessage();
   }        
  }

  public function __destruct()
  {
    // Disconnect from DB
    $this->pdo = null;
    //echo 'Successfully disconnected from the database!';
  }
}
?>
<?php

// Database connection PDO

class Database {

    public function __construct($host, $dbname, $user, $pass) {
        // Attempt DB connection
        try
        {
            $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

这使得类更易于移植,也更易于测试。

我建议您按照如下方式重新编写类

class.Connect.php

<?php

// Database connection PDO

class Database {

  public function __construct() {

  // Connection information
  $host   = 'localhost';
  $dbname = 'imanage';
  $user   = 'root';
  $pass   = '';

  // Attempt DB connection
  try
  {
    $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //echo 'Successfully connected to the database!';
   }
   catch(PDOException $e)
   {
    echo $e->getMessage();
   }        
  }

  public function __destruct()
  {
    // Disconnect from DB
    $this->pdo = null;
    //echo 'Successfully disconnected from the database!';
  }
}
?>
<?php

// Database connection PDO

class Database {

    public function __construct($host, $dbname, $user, $pass) {
        // Attempt DB connection
        try
        {
            $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

这使得类更易于移植,也更易于测试。

在代码中,您添加的注释是:

class Register {
    public function __construct()
         {
           $this->pdo = new Database($host, $dbname, $user, $pass); 
           // ofcourse you can get the db connections details and database 
           // name from a config file
       }
但这是一个物体,所以不,你实际上不能。一个物体基本上对外界一无所知,除非你明确告诉它

因此,除非您想使用globals或其他类似的丑陋的东西,否则您需要修改创建新对象的方式:

$this->pdo = new Database();

您可以这样做,因为您的数据库类中已经包含了详细信息(其次,您的Register对象不知道它们,或者您必须在Register对象中声明它们,并修改数据库类的构造函数以接受输入。

在您的代码中,您添加的注释是:

class Register {
    public function __construct()
         {
           $this->pdo = new Database($host, $dbname, $user, $pass); 
           // ofcourse you can get the db connections details and database 
           // name from a config file
       }
但这是一个物体,所以不,你实际上不能。一个物体基本上对外界一无所知,除非你明确告诉它

因此,除非您想使用globals或其他类似的丑陋的东西,否则您需要修改创建新对象的方式:

$this->pdo = new Database();

您可以这样做,因为您的数据库类中已经包含了详细信息(其次,您的Register对象不知道它们,或者您必须在Register对象中声明它们,并修改数据库类的构造函数以接受输入。

您的数据库类没有“准备”方法

因此,此代码:
$stmt=$this->pdo->prepare($sql);
将导致您收到的致命错误。
$this->pdo
仅包含数据库类的实例

Fatal error: Call to undefined method Database::prepare() in E:\xampp\htdocs\attendance\class.Register.php on line 17

您可以在Register类中执行
$stmt=$this->pdo->pdo->prepare($sql);
。这将消除您的错误。
$this->pdo
将是数据库类的实例,然后该类中的属性pdo包含pdo类的实例。

您的数据库类没有“prepare”方法

因此,此代码:
$stmt=$this->pdo->prepare($sql);
将导致您收到的致命错误。
$this->pdo
仅包含数据库类的实例

Fatal error: Call to undefined method Database::prepare() in E:\xampp\htdocs\attendance\class.Register.php on line 17

您可以执行
$stmt=$this->pdo->pdo->prepare($sql);
在您的Register类中。这将消除您的错误。
$This->pdo
将是您的数据库类的实例,然后该类中的属性pdo包含您的pdo类的实例。

我已经按照您上面的要求完成了操作,仍然得到相同的结果errors@gutigrewal好的…现在你的
数据库
班级合作构造函数不接受任何参数,因此在创建
数据库
类的对象时避免传递参数..我如何做?通过删除db类中的try和catch?我已经尝试过了,但仍然不起作用,您可以给我一个例子吗?@gutigrewal您可以看到编辑过的答案…我在创建对象时删除了参数。。。因为您已经在
数据库
类中设置了变量…请参见我编辑的答案。很抱歉,我错过了,我已经完成了,并且它仍然纠正了大多数错误,但是它说的是
致命错误:调用未定义的方法数据库::prepare()
我不明白,如果它连接到我的数据库类,它应该知道这是一个标准方法?我已经按照您上面的要求完成了,我仍然得到了相同的结果errors@gutigrewal好的…现在您的
数据库
类构造函数不接受任何参数,因此在创建
数据库
类的对象时避免传入g arguments..我怎么做?通过删除db类中的try and catch?我已经尝试过了,但仍然不起作用,你能给我一个例子吗?@gutigrewal你可以看到编辑过的答案…我在创建对象时删除了参数…因为你已经在
数据库
类中设置了变量…看到我编辑过的答案。对不起,我很抱歉