Petja
Petja

Reputation: 594

Getting error in PDO with OOP

I'm definitely beginner in PDO and object-oriented programming at all.

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 "?";
        }
    }
}

And this is my table's dump:

CREATE TABLE IF NOT EXISTS `asiakkaat` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstname` tinytext NOT NULL,
  `lastname` tinytext NOT NULL,
  `password` tinyblob NOT NULL,
  `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` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `ready` tinyint(1) NOT NULL DEFAULT '0',
  `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;

INSERT INTO `customers` (`id`, `firstname`, `lastname`, `password`, `address`, `postalcode`, `city`, `companyid`, `company`, `domain`, `tickets`, `project_started`, `ready`, `timestamp`) VALUES
(1, 'Linus', 'Torvalds', NULL, 'Example address', '12345', 'Example', '1234-1234-12', 'Linux', 'linux.com', 0, '2012-07-23 20:41:57', 1, '2012-06-28 20:41:57'),
(2, 'Bill', 'Gates', 0x30, 'Sesame str.', '12345', 'Example', '1234-1234-12', 'Microsoft corp.', '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');

I've set PHP's display_errors on and it looks like error is on line 25. But I don't get what's wrong?

The code I use to activate my script is:

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

Upvotes: 0

Views: 263

Answers (1)

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

Based on comments:

The activation code for your functions is wrong.

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

You're misunderstanding the concept of inheritence in OOP.

Since information extends mysql, all of the public/protected methods and fields are inherited to information. Meaning, you should be doing the following:

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

This will set $information's $db field (and not a different object), each object is its own entity.

Upvotes: 1

Related Questions