Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.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 使用OOP获取PDO中的错误_Php_Mysql_Oop_Pdo - Fatal编程技术网

Php 使用OOP获取PDO中的错误

Php 使用OOP获取PDO中的错误,php,mysql,oop,pdo,Php,Mysql,Oop,Pdo,我绝对是PDO和面向对象编程的初学者 class mysql { public $db; public function connect() { $this->db = new PDO( "mysql:host=localhost;dbname=mydbname;", "root", "" ); } } class information extends my

我绝对是PDO和面向对象编程的初学者

class mysql {
    public $db;

    public function connect() {
        $this->db = new PDO(
            "mysql:host=localhost;dbname=mydbname;",
            "root",
            ""
        );
    }
}

class information extends mysql {
    public $customer_count;
    public $statement;
    public $query;

    public function customer_queue($asid = false){
        try{
            if($asid == false){
                $this->query = "SELECT COUNT(*) FROM `customers` WHERE `ready` = '0' ORDER BY `id` ASC";
            }else{
                $this->query = "SELECT COUNT(*) FROM `customers` WHERE `ready` = '0' AND `id` < ':asid' ORDER BY `id` ASC";
            }
            $this->statement = $this->db->prepare($this->query);
            $this->statement->execute(array(
                "asid" =>           $asid
            ));
            $this->customer_count = $this->statement->fetchColumn();
            return $this->customer_count;
        }catch(PDOException $e){
            return "?";
        }
    }
}
根据评论:

您的功能的激活代码错误

$mysql = new mysql();
$mysql->connect();
$information = new information();
print $information->queue();
您误解了OOP中继承的概念

由于
information扩展了mysql
,所有公共/受保护的方法和字段都继承到
information
。也就是说,您应该执行以下操作:

$information = new information;
$information->connect();
print $information->queue();

这将设置
$information
$db
字段(而不是不同的对象),每个对象都是它自己的实体。

在尝试运行查询之前是否确实调用了
mysql::connect()
?@Nile
$This->statement=$This->db->prepare($This->query)
@ErmSo
致命错误:调用C:\server\project\u name\classes\core.php第25行的非对象上的成员函数prepare()
@PetjaTouru:该函数应根据您的代码设置
$this->db
。我想那不是你造的吧?请告诉我们您正在尝试的代码execute@Truth你的意思是:
$mysql=newmysql()$mysql->connect()$信息=新信息();打印$information->queue()是的,我有
mysql::connect()
$mysql = new mysql();
$mysql->connect();
$information = new information();
print $information->queue();
$information = new information;
$information->connect();
print $information->queue();