Php 使用OOP获取PDO中的错误
我绝对是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
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();