0

我绝对是 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 "?";
        }
    }
}

这是我桌子的转储:

如果不存在 `asiakkaat` 则创建表 (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` tinytext NOT NULL,
  `lastname` tinytext NOT NULL,
  `密码` tinyblob 不为空,
  `address` tinytext NOT NULL,
  `postalcode` tinytext NOT NULL,
  `city` tinytext NOT NULL,
  `companyid` tinytext NOT NULL,
  `company` tinytext NOT NULL,
  `domain` tinytext NOT NULL,
  `tickets` int(11) NOT NULL DEFAULT '0',
  `project_started` 时间戳 NOT NULL DEFAULT '0000-00-00 00:00:00',
  `ready` tinyint(1) NOT NULL DEFAULT '0',
  `timestamp` 时间戳 NOT NULL DEFAULT CURRENT_TIMESTAMP,
  主键(`id`)
) 引擎=InnoDB 默认字符集=utf8 AUTO_INCREMENT=6 ;

INSERT INTO `customers` (`id`, `firstname`, `lastname`, `password`, `address`, `postalcode`, `city`, `companyid`, `company`, `domain`, `tickets`, `project_started`, `ready`, `timestamp`) 值
(1,'Linus','Torvalds',NULL,'示例地址','12345','示例','1234-1234-12','Linux','linux.com',0,'2012-07 -23 20:41:57', 1, '2012-06-28 20:41:57'),
(2,'比尔','盖茨',0x30,'芝麻街','12345','示例','1234-1234-12','微软公司','microsoft.com',0,' 2012-07-30 07:47:36', 1, '2012-06-29 07:47:36'),
(3, 'David', 'Axmark', 0x30, 'MySQL''s street 5', '12345', 'MySQL', '1234-1234-12', 'MySQL', 'mysql.com', 0, '2012-08-01 07:54:00', 0, '2012-06-29 07:54:00'),
(4, 'Michael', 'Widenius', 0x30, 'MySQL''s street 6', '12345', 'MySQL', '1234-1234-12', 'MySQL', 'mysql.com', 0, '0000-00-00 00:00:00', 0, '2012-06-29 07:59:48'),
(5, 'Larry', 'Page', 0x30, 'Something way', '12345', 'Nothing', '1234-1234-12', 'Google Inc.', 'google.com', 0, '0000 -00-00 00:00:00', 0, '2012-06-29 07:59:48');

我已将 PHP 设置为display_errorson,看起来错误在第 25 行。但我不明白怎么了?

我用来激活我的脚本的代码是:

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

1 回答 1

1

根据评论:

您的功能的激活码错误。

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

您误解了OOP中的继承概念。

因为information extends mysql,所有公共/受保护的方法和字段都继承到information. 意思是,您应该执行以下操作:

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

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

于 2012-08-05T17:59:45.290 回答