Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO类未返回计数_Php_Mysql_Pdo - Fatal编程技术网

Php PDO类未返回计数

Php PDO类未返回计数,php,mysql,pdo,Php,Mysql,Pdo,我用StackOverflow的一些教程和代码编写了一个PDO抽象类,以使我的生活变得更轻松,然而PDO仍然是一个令人头疼的问题,让我怀疑我是否愚蠢,或者PDO的学习曲线是否比好的老MySQL更大 无论如何,我要做的是创建一个统计类来计算几行,而不编写左右主要查询。我正试图计算以下表格的数量联系人+公司+用户 但由于某种原因它不起作用。大多数时候我都犯了500个错误。从代码上看,它在大多数情况下似乎是正确的,除非我遗漏了什么 下面是数据库抽象类lib/Database.php class Dat

我用StackOverflow的一些教程和代码编写了一个PDO抽象类,以使我的生活变得更轻松,然而PDO仍然是一个令人头疼的问题,让我怀疑我是否愚蠢,或者PDO的学习曲线是否比好的老MySQL更大

无论如何,我要做的是创建一个统计类来计算几行,而不编写左右主要查询。我正试图计算以下表格的数量联系人+公司+用户

但由于某种原因它不起作用。大多数时候我都犯了500个错误。从代码上看,它在大多数情况下似乎是正确的,除非我遗漏了什么

下面是数据库抽象类lib/Database.php

class Database{
    private $host      = DB_HOST;
    private $user      = DB_USER;
    private $pass      = DB_PASS;
    private $dbname    = DB_NAME;

    private $dbh;
    private $error;
    private $stmt;

    public function __construct(){
        // Set DSN
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        // Set options
        $options = array(
            PDO::ATTR_PERSISTENT    => true,
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
        );
        // Create a new PDO instanace
        try{
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        }
        // Catch any errors
        catch(PDOException $e){
            $this->error = $e->getMessage();
        }
    }

    # PDO Prepare
    public function query($query){
    $this->stmt = $this->dbh->prepare($query);
    }

    # PDO Count All
    public function countAll($value){
    $sql = "SELECT * FROM `$value`";

    $this->stmt = $this->dbh->prepare($sql);
    try { $this->stmt = $this->execute(); } catch(PDOException $e) { $this->error = $e->getMessage(); }
    return $this->stmt->rowCount();
    }

    # PDO Bind
    public function bind($param, $value, $type = null){
    if (is_null($type)) {
        switch (true) {
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
            default:
                $type = PDO::PARAM_STR;
        }
    }
    $this->stmt->bindValue($param, $value, $type);
    }

    # PDO Execute
    public function execute(){
    return $this->stmt->execute();
    }

    # PDO Multiple Records
    public function resultset(){
    $this->execute();
    return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    # PDO Single Record
    public function single(){
    $this->execute();
    return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    # PDO Count
    public function rowCount(){
    return $this->stmt->rowCount();
    }

    # PDO Last Insert ID
    public function lastInsertId(){
    return $this->dbh->lastInsertId();
    }

    # PDO Transactions begin / end / cancel
    public function beginTransaction(){
    return $this->dbh->beginTransaction();
    }

    public function endTransaction(){
    return $this->dbh->commit();
    }

    public function cancelTransaction(){
    return $this->dbh->rollBack();
    }

    # PDO Debug Dump
    public function debugDumpParams(){
    return $this->stmt->debugDumpParams();
    }

}
class Stats{
      private $_db;

      public function __construct(Database $db){
         $this->_db = $db;
      }

      public function countContacts() {
          $this->_db->query('select count(*) from contacts');
          $this->_db->fetchColumn();
      }

      public function countCompanies() {
          $this->_db->query('select count(*) from companies');
          $this->_db->fetchColumn();
      }

      public function countUsers() {
          $this->_db->query('select count(*) from users');
          $this->_db->fetchColumn();
      }

      public function countInvoices() {
          $this->_db->query('select count(*) from invoices');
          $this->_db->fetchColumn();
      }
}
$database = new Database();
$stats = new Stats($database); 
echo $stats->countContacts();
这里是Statistics类lib/Stats.class.php

class Database{
    private $host      = DB_HOST;
    private $user      = DB_USER;
    private $pass      = DB_PASS;
    private $dbname    = DB_NAME;

    private $dbh;
    private $error;
    private $stmt;

    public function __construct(){
        // Set DSN
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        // Set options
        $options = array(
            PDO::ATTR_PERSISTENT    => true,
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
        );
        // Create a new PDO instanace
        try{
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        }
        // Catch any errors
        catch(PDOException $e){
            $this->error = $e->getMessage();
        }
    }

    # PDO Prepare
    public function query($query){
    $this->stmt = $this->dbh->prepare($query);
    }

    # PDO Count All
    public function countAll($value){
    $sql = "SELECT * FROM `$value`";

    $this->stmt = $this->dbh->prepare($sql);
    try { $this->stmt = $this->execute(); } catch(PDOException $e) { $this->error = $e->getMessage(); }
    return $this->stmt->rowCount();
    }

    # PDO Bind
    public function bind($param, $value, $type = null){
    if (is_null($type)) {
        switch (true) {
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
            default:
                $type = PDO::PARAM_STR;
        }
    }
    $this->stmt->bindValue($param, $value, $type);
    }

    # PDO Execute
    public function execute(){
    return $this->stmt->execute();
    }

    # PDO Multiple Records
    public function resultset(){
    $this->execute();
    return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    # PDO Single Record
    public function single(){
    $this->execute();
    return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    # PDO Count
    public function rowCount(){
    return $this->stmt->rowCount();
    }

    # PDO Last Insert ID
    public function lastInsertId(){
    return $this->dbh->lastInsertId();
    }

    # PDO Transactions begin / end / cancel
    public function beginTransaction(){
    return $this->dbh->beginTransaction();
    }

    public function endTransaction(){
    return $this->dbh->commit();
    }

    public function cancelTransaction(){
    return $this->dbh->rollBack();
    }

    # PDO Debug Dump
    public function debugDumpParams(){
    return $this->stmt->debugDumpParams();
    }

}
class Stats{
      private $_db;

      public function __construct(Database $db){
         $this->_db = $db;
      }

      public function countContacts() {
          $this->_db->query('select count(*) from contacts');
          $this->_db->fetchColumn();
      }

      public function countCompanies() {
          $this->_db->query('select count(*) from companies');
          $this->_db->fetchColumn();
      }

      public function countUsers() {
          $this->_db->query('select count(*) from users');
          $this->_db->fetchColumn();
      }

      public function countInvoices() {
          $this->_db->query('select count(*) from invoices');
          $this->_db->fetchColumn();
      }
}
$database = new Database();
$stats = new Stats($database); 
echo $stats->countContacts();
下面是我如何调用sayindex.php

class Database{
    private $host      = DB_HOST;
    private $user      = DB_USER;
    private $pass      = DB_PASS;
    private $dbname    = DB_NAME;

    private $dbh;
    private $error;
    private $stmt;

    public function __construct(){
        // Set DSN
        $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
        // Set options
        $options = array(
            PDO::ATTR_PERSISTENT    => true,
            PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
        );
        // Create a new PDO instanace
        try{
            $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        }
        // Catch any errors
        catch(PDOException $e){
            $this->error = $e->getMessage();
        }
    }

    # PDO Prepare
    public function query($query){
    $this->stmt = $this->dbh->prepare($query);
    }

    # PDO Count All
    public function countAll($value){
    $sql = "SELECT * FROM `$value`";

    $this->stmt = $this->dbh->prepare($sql);
    try { $this->stmt = $this->execute(); } catch(PDOException $e) { $this->error = $e->getMessage(); }
    return $this->stmt->rowCount();
    }

    # PDO Bind
    public function bind($param, $value, $type = null){
    if (is_null($type)) {
        switch (true) {
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
            default:
                $type = PDO::PARAM_STR;
        }
    }
    $this->stmt->bindValue($param, $value, $type);
    }

    # PDO Execute
    public function execute(){
    return $this->stmt->execute();
    }

    # PDO Multiple Records
    public function resultset(){
    $this->execute();
    return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    # PDO Single Record
    public function single(){
    $this->execute();
    return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    # PDO Count
    public function rowCount(){
    return $this->stmt->rowCount();
    }

    # PDO Last Insert ID
    public function lastInsertId(){
    return $this->dbh->lastInsertId();
    }

    # PDO Transactions begin / end / cancel
    public function beginTransaction(){
    return $this->dbh->beginTransaction();
    }

    public function endTransaction(){
    return $this->dbh->commit();
    }

    public function cancelTransaction(){
    return $this->dbh->rollBack();
    }

    # PDO Debug Dump
    public function debugDumpParams(){
    return $this->stmt->debugDumpParams();
    }

}
class Stats{
      private $_db;

      public function __construct(Database $db){
         $this->_db = $db;
      }

      public function countContacts() {
          $this->_db->query('select count(*) from contacts');
          $this->_db->fetchColumn();
      }

      public function countCompanies() {
          $this->_db->query('select count(*) from companies');
          $this->_db->fetchColumn();
      }

      public function countUsers() {
          $this->_db->query('select count(*) from users');
          $this->_db->fetchColumn();
      }

      public function countInvoices() {
          $this->_db->query('select count(*) from invoices');
          $this->_db->fetchColumn();
      }
}
$database = new Database();
$stats = new Stats($database); 
echo $stats->countContacts();
连接值在后台传递,因为它们包含在模板文件的头中


关于我做错了什么有什么建议吗?

出于某种原因,您刚刚忘记了
query()
函数实现,它只不过是
PDO::prepare()
的包装。所以用你现在的方式来称呼它是没有意义的

将以下方法添加到类中

public function run($query, $params = NULL){
    $stmt = $this->dbh->prepare($query);
    $stmt->execute($params);
    return $stmt;
}
然后重写您的统计数据收集器:

  public function countUsers() {
      return $this->_db->run('select count(*) from users')->fetchColumn();
  }

请注意,包装器中的所有其他函数要么有害,要么无用。请阅读我的文章,找出为什么出于某种原因您忘记了
query()
函数实现,它只不过是
PDO::prepare()
的包装。所以用你现在的方式来称呼它是没有意义的

将以下方法添加到类中

public function run($query, $params = NULL){
    $stmt = $this->dbh->prepare($query);
    $stmt->execute($params);
    return $stmt;
}
然后重写您的统计数据收集器:

  public function countUsers() {
      return $this->_db->run('select count(*) from users')->fetchColumn();
  }

请注意,包装器中的所有其他函数要么有害,要么无用。请阅读我的文章,找出为什么打开错误报告-它说了什么?500错误:服务器错误;检查日志。@Fred ii-我知道500是服务器错误,但Apache错误日志中没有关于500错误的时间戳的错误日志。但是我发现这个。。。PHP解析错误:第15行的/Applications/MAMP/htdocs/sbs/lib/stats.class.PHP中出现语法错误、意外的“,”和预期的变量(T_variable),您是否尝试过使用
fetch()
而不是
fetchColumn()
?执行
应该在
$this->stmt
上,而不是
$this
。第15行是哪一行?打开错误报告-它说什么?500错误:服务器错误;检查日志。@Fred ii-我知道500是服务器错误,但Apache错误日志中没有关于500错误的时间戳的错误日志。但是我发现这个。。。PHP解析错误:第15行的/Applications/MAMP/htdocs/sbs/lib/stats.class.PHP中出现语法错误、意外的“,”和预期的变量(T_variable),您是否尝试过使用
fetch()
而不是
fetchColumn()
?执行
应该在
$this->stmt
上,而不是
$this
。第15行是哪一行?对于数据库类的th__构造部分,您有什么建议?对于数据库类的th__构造部分,您有什么建议?