【问题标题】:how to call on my database class from a different class如何从不同的类调用我的数据库类
【发布时间】:2013-09-18 09:20:31
【问题描述】:

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

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

Notice: Undefined variable: host in E:\xampp\htdocs\attendance\class.Register.php on line 10

Notice: Undefined variable: dbname in E:\xampp\htdocs\attendance\class.Register.php on line 10

Notice: Undefined variable: user in E:\xampp\htdocs\attendance\class.Register.php on line 10

Notice: Undefined variable: pass in E:\xampp\htdocs\attendance\class.Register.php on line 10

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

但是我已经在我的数据库类中定义了这些,并告诉我的注册类需要连接文件。我究竟做错了什么?我似乎无法弄清楚我应该如何在我的注册类中调用我的数据库连接?有人有什么想法吗?

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!';
  }
}
?>

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();
?>

【问题讨论】:

  • 包含文件中的变量不存在于对象方法中。 $user、$pass 和 $host 在你的构造函数中什么都不是
  • 也许this 有帮助。

标签: php mysql pdo


【解决方案1】:

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

如下定义类属性。

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

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

【讨论】:

  • 我已经按照你上面的要求完成了,但我仍然遇到同样的错误
  • @gutigrewal 好的...现在您的Database 类构造函数不接受任何参数,因此在创建Database 类的对象期间避免传递参数..
  • 我该怎么做?通过删除 db 类中的 try 和 catch ?我试过了,还是不行,你能举个例子吗?
  • @gutigrewal 您可以看到编辑后的答案...我在创建对象期间删除了参数...因为您已经在Database 类中设置了变量...请参阅我编辑的答案。
  • 对不起我错过了,我已经做到了,它仍然纠正了大部分错误但是它说Fatal error: Call to undefined method Database::prepare()我不明白如果它连接到我的数据库类它应该知道这是标准方法吗?
【解决方案2】:

让我建议您按如下方式重新编写这些类。

class.Connect.php

<?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 {
        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

然后像这样使用这些类。

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

$database = new Database($host, $dbname, $user, $pass);
$register = new Register($database);

这使类更易于移植,并且更易于测试。

【讨论】:

    【解决方案3】:

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

    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
           }
    

    但这是一个对象,所以不,你实际上不能。一个对象基本上对外部世界一无所知,除非你明确告诉它。

    因此,除非您想使用全局变量或其他类似的丑陋事物,否则您将需要修改创建新对象的方式:

    $this->pdo = new Database();
    

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

    【讨论】:

      【解决方案4】:

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

      因此,此代码:$stmt = $this-&gt;pdo-&gt;prepare($sql); 将导致您遇到的致命错误。 $this-&gt;pdo 仅包含您的 Database 类的实例。

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

      您可以在 Register 类中使用$stmt = $this-&gt;pdo-&gt;pdo-&gt;prepare($sql);。这将消除您遇到的错误。 $this-&gt;pdo 将是您的 Database 类的实例,然后该类中的属性 pdo 包含您的 pdo 类的实例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-31
        • 2019-07-14
        相关资源
        最近更新 更多