【问题标题】:Interaction between two objects in php using PDOphp中两个对象使用PDO进行交互
【发布时间】:2012-01-18 02:31:51
【问题描述】:

我正在尝试将从 Datamanager 对象中的 PDO 收集的值传递给 User 对象。

  1. 最初调用用户对象,请求检索具有 ID 的用户。
  2. 用户在构造中创建与数据管理器的连接。
  3. datamanager 构造调用 PDO 连接。
  4. 运行用户选择会创建要在数据库上执行的查询。
  5. 然后将查询传递给处理查询并返回结果的数据管理器。
  6. 应将此结果传递/设置给用户对象。

我不确定是第 5 步还是第 6 步错了。

class Datamanager {
public function __construct() {
    try {
        $this->dbh = new PDO("mysql:host=$this->hostName;dbname=$this->dbName", $this->dbUser, $this->dbPassword);
    } catch(PDOException $e) {
        echo $e->getMessage();
    }
}
public function runQuery($query, $obj){
    try {
        $STH = $this->dbh->query($query);
        $STH->setFetchMode(PDO::FETCH_INTO, $obj);
    } catch(PDOException $e) {
        echo $e->getMessage();
    }
    return($obj);
}
class User {
public $user_id;
public $user_name;
public $user_password;
public $session_id;
private $dbc;

public function __construct() {
    $this->dbc = new Datamanager();
}

function selectUser ($userID, $obj) {
    $query = 'SELECT user_id, user_name, user_password, session_id FROM users';
    $results = $this->dbc->runQuery($query, $this);
}

$userID = "1";

$test = new User;

$test->selectUser($userID, $test);

我运行用户的初始设置,然后尝试使用在数据管理器中检索到的值加载它。我知道 PDO 本身就是一个对象,但我仍在尝试创建一个通用交互,因为此代码将被扩展以允许更多用户对象访问数据库。

【问题讨论】:

    标签: php oop pdo


    【解决方案1】:

    这是我的版本。

    class Datamanager {
    
        public $hostName = 'localhost';
        public $dbName = 'stackoverflow';
        public $dbUser = 'xxx';
        public $dbPassword = 'xxx';
    
        public function __construct() {
            try {
                $this->dbh = new PDO("mysql:host=$this->hostName;dbname=$this->dbName", $this->dbUser, $this->dbPassword);
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }
    
        public function runQuery($query, $obj) {
            try {
                $STH = $this->dbh->prepare($query);
                $STH->bindParam(':id', $obj->user_id);
    
                $STH->setFetchMode(PDO::FETCH_INTO, $obj);
                $STH->execute();
                $STH->fetch();
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
            return $obj;
        }
    
    }
    
    class User {
    
        public $user_id;
        public $user_name;
        public $user_password;
        public $session_id;
        private $dbc;
    
        public function __construct() {
            $this->dbc = new Datamanager();
        }
    
        function selectUser($userID, $obj) {
            $this->user_id = $userID;
            $query = 'SELECT user_id, user_name, user_password, session_id FROM users WHERE user_id=:id';
            $results = $this->dbc->runQuery($query, $this);
        }
    
    }
    
    $userID = "1";
    $test = new User;
    
    $test->selectUser($userID, $test);
    
    var_dump($test);
    

    我在您的查询中添加了 WHERE user_id=:id,并将 pdo 更改为使用 prepare()

    【讨论】:

      【解决方案2】:

      您缺少几个结束的 }。这只是复制和粘贴错误吗?

      另外,这行不应该:

      $results = $this->dbc->runQuery($query, $this);
      

      改为:

      $results = $this->dbc->runQuery($query, $obj);
      

      【讨论】:

        猜你喜欢
        • 2021-06-02
        • 2013-07-02
        • 2011-10-11
        • 2011-03-13
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 2014-07-12
        • 1970-01-01
        相关资源
        最近更新 更多