0

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();
6
  • Are you sure you've called mysql::connect() before attempting to run the query? Commented Aug 5, 2012 at 17:40
  • @Nile $this->statement = $this->db->prepare($this->query); Commented Aug 5, 2012 at 17:49
  • @ErmSo Fatal error: Call to a member function prepare() on a non-object in C:\server\project_name\classes\core.php on line 25 Commented Aug 5, 2012 at 17:50
  • @PetjaTouru: That's the function that should set $this->db according to your code. I take it that you didn't build that? Please show us the code you are trying to execute Commented Aug 5, 2012 at 17:53
  • @Truth Did you meant this: $mysql = new mysql();$mysql->connect();$information = new information();print $information->queue(); Yep, I have mysql::connect() Commented Aug 5, 2012 at 17:57

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.