PDO prepare方法从一个类到另一个类是未定义的

时间:2016-06-09 13:08:44

标签: php class pdo prepared-statement

这是我的代码

class db{
public function connect(){
      try{
        $db = new PDO("mysql:host=$host; dbname = $dbname", $user , $pass);        
        return this->$db;
      }
     catch (PDOException $e){
       echo $e->getMessage();        
     } 
  }

那么我有另一个类来从数据库中获取一些数据

class getData{

public function getData($table,$where) {
//$where is dynamic and is like array('user'=>$username,'pass'=>$password))

   $db = new db();

    foreach ($where as $value){
        $where_string.=(" AND ".$value."=:".$value);                
    } 
    $where_string=substr($where_string,5);
  // end up with foo=:foo AND foo=:foo to add in the query

    $query_array = array();
    foreach ($where as $value){     
        $query_array[$value]= $value;
    }
  //end up with array(foo=>foo, foo=>foo); to add in the query


    $statement = $db->prepare("select id from table = :table where " .$where_string);
    $statement->execute($query_array);
    $row = $statement->fetch(); 

  }

}

$where中的getData var未修复,因此我必须循环它并构造查询的where部分和预准备语句的绑定部分。

我总是以Fatal error: Call to undefined method db::prepare() in /home/projectAwesome/public_html/mystuff.php on line 241

结束

我该如何解决这个问题? PHP应该看到prepare方法,因为它是php的全局模块并且正在运行。 感谢

更新

现在我的代码是

class db extends PDO{
  private $host;
  private $user;
  private $pass;
  private $name;
  private $link;
  private $error;
  private $errno;
  private $query;
  function __construct($myhost='localhost', $myuser='aaa', $mypass='bbb', $myname='ccc', $myconn = 1) {
    $this -> host = $myhost;
    $this -> user = $myuser;
    $this -> pass = $mypass;
    if (!empty($myname)) $this -> name = $myname;      
    if ($myconn == 1) $this -> connect();
  }
  function __destruct() {
    @mysql_close($this->link);
  }
  public function connect(){
      try{
        $host = $this -> host;
        $dbname = $this -> name;
        $user = $this -> user;
        $pass = $this -> pass;   
        $charset = 'utf8';

      $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
      $opt = [
       PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
       PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
       PDO::ATTR_EMULATE_PREPARES   => false,
      ];  
      $db = new PDO($dsn, $user, $pass, $opt);
        return $db;
      }
     catch (PDOException $e){
             echo $e->getMessage();
     } 
  }
}

然后是另一个班级

class getData{
public function getData($table,$where) {
$db = new db();

    $where_string   = "";
    foreach ($where as $key=>$value){
        $where_string.=($value."=:".$value." AND ");                
     }
    $where_string=rtrim($where_string, " AND ");



    $query_array = array();
    foreach ($where as $value){
           $query_array[$value]= $value;
    }


    $statement = $db->prepare("select id from" .$table. "where ".$where_string);
    $statement->execute($query_array);
    $row = $statement->fetch(); 


  }
}

现在错误

Warning: PDO::prepare(): SQLSTATE[00000]: No error: PDO constructor was not called in /home/... on line 254

Fatal error: Call to a member function execute() on null in /home/... on line 255

请指教。谢谢

1 个答案:

答案 0 :(得分:2)

您可能会发现一些疏忽。 1.)db类中的connect()方法无法访问变量:$ host,$ user,$ pass等.2。)getData类中的getData方法实例化db类而没有任何方法db类获取有关$ host,$ user,$ pass等变量的信息。将这些变量定义为常量可能是理想的,然后您不必一直传递它们。否则,这可以帮到你:

    <?php

        class db extends PDO{
            public function connect($host, $dbname, $user, $pass) {
                try {
                    $db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

                    return $this->$db;
                } catch (PDOException $e) {
                    echo $e->getMessage();
                }
            }
        }

        class getData{

            public function getData($table, $where, $host, $dbname, $user, $pass) {
                //$where is dynamic and is like array('user'=>$username,'pass'=>$password))                 
                $db             = new db($host, $dbname, $user, $pass);
                $where_string   = "";

                foreach ($where as $key=>$value){
                    $where_string .= ($value."=:".$value . " AND ");
                }
                // REMOVE SURPLUS " AND " TO THE RIGHT OF THE STRING... 
                $where_string = rtrim($where_string, " AND ");
                // $where_string = substr($where_string,5);  //<== WHY substr? WHAT'S YOUR INTENTION HERE? 
                // end up with foo=:foo AND foo=:foo to add in the query

                $query_array    = array();
                foreach ($where as $value){
                    $query_array[$value]= $value;
                }
                //end up with array(foo=>foo, foo=>foo); to add in the query


                $statement  = $db->prepare("select id from table " . $table . " where " . $where_string);
                $statement->execute($query_array);
                $row        = $statement->fetch();

            }

        }

更新:DB CLASS

    <?php
        class db extends PDO{
            private $host;
            private $user;
            private $pass;
            private $dbh;
            private $name;
            private $link;
            private $error;
            private $errno;
            private $query;
            function __construct($myhost='localhost', $myuser='aaa', $mypass='bbb', $myname='ccc', $myconn = 1) {
                $this -> host = $myhost;
                $this -> user = $myuser;
                $this -> pass = $mypass;

                if (!empty($myname)){
                    $this->name = $myname;
                }
                if ($myconn == 1){
                    if(isset($this->dbh)){
                        return $this->dbh;
                    }
                    return $this->connect();
                }
            }
            function __destruct() {
                $this->dbh  = null;
            }
            public function connect(){
                try{
                    $host       = $this -> host;
                    $dbname     = $this -> name;
                    $user       = $this -> user;
                    $pass       = $this -> pass;
                    $charset    = 'utf8';

                    $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
                    $opt = [
                        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                        PDO::ATTR_EMULATE_PREPARES   => false,
                    ];
                    $this->dbh  = new PDO($dsn, $user, $pass, $opt);
                    return $this->dbh ;
                }
                catch (PDOException $e){
                    echo $e->getMessage();
                }
            }
        }