0

I have a database class which successfully connects to my localhost and to my database, I want to select all the users in my users database however when I am calling my database this is proving to be more difficult that I thought it would be.

I'm getting numerous errors for example:

Notice: Undefined variable: host in E:\xampp\htdocs\attendance\class.Register.php on line 10

Notice: Undefined variable: dbname in E:\xampp\htdocs\attendance\class.Register.php on line 10

Notice: Undefined variable: user in E:\xampp\htdocs\attendance\class.Register.php on line 10

Notice: Undefined variable: pass in E:\xampp\htdocs\attendance\class.Register.php on line 10

Fatal error: Call to undefined method Database::prepare() in E:\xampp\htdocs\attendance\class.Register.php on line 17

However I've defined these in my database class and have told my register class to require the connect file. What am I doing wrong? I can't seem to work out how I should call my database connection in my register class? Anyone have any ideas?

class.Connect.php

<?php

// Database connection PDO

class Database {

  public function __construct() {

  // Connection information
  $host   = 'localhost';
  $dbname = 'imanage';
  $user   = 'root';
  $pass   = '';

  // Attempt DB connection
  try
  {
    $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
    $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //echo 'Successfully connected to the database!';
   }
   catch(PDOException $e)
   {
    echo $e->getMessage();
   }        
  }

  public function __destruct()
  {
    // Disconnect from DB
    $this->pdo = null;
    //echo 'Successfully disconnected from the database!';
  }
}
?>

class.Register.php

<?php

require 'class.Connect.php';
class Register {

    public function __construct()
    {
           $this->pdo = new Database($host, $dbname, $user, $pass); //ofcourse you can get the db connections details and database name from a config file
    }

        public function viewall() {

    $sql = "SELECT * FROM users";
    $stmt = $this->pdo->prepare($sql);
    $stmt->execute();
    // here you go:
    $users = $stmt->fetchAll();

    foreach ($users as $row) {
        print $row["firstname"] . "-" . $row["lastname"] ."<br/>";
    }
    }
}
$run = new Register();
$run->viewall();
?>
plain jane
  • 1,009
  • 1
  • 8
  • 19
user0129e021939232
  • 6,205
  • 24
  • 87
  • 140

4 Answers4

2

You are using $this->pdo for defining your database connection but forget to define property for class.

define class perperty as below.

class Register
{
    private  $pdo;
    public function __construct()
    {
         $this->pdo = new Database();    
    }

your Database class constructor not accepting any argument so during creating object of Database class avoid passing arguments.

Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
  • I've done as you have requested above and I still get the same errors – user0129e021939232 Sep 18 '13 at 09:24
  • @gutigrewal Okay...now your `Database` class constructor not accepting any argument so during creating object of `Database` class avoid passing arguments.. – Dipesh Parmar Sep 18 '13 at 09:26
  • how can i do that? by removing the try and catch in the db class? I've tried that and that still didn't work, could you perhaps give me an example? – user0129e021939232 Sep 18 '13 at 09:28
  • @gutigrewal you can see edited answer...i removed argument during creating object...because you already have variable set in `Database` class...see my edited answer. – Dipesh Parmar Sep 18 '13 at 09:30
  • sorry I missed that, i've done that and it still has corrected majority of the errors however it is saying `Fatal error: Call to undefined method Database::prepare()` i don't understand that if its connected to my database class it should know that this is a standard method? – user0129e021939232 Sep 18 '13 at 09:39
  • @gutigrewal its quite obvious because your `$this->pdo` is object of `Database` Class not of the actual PDO...so it will look for the prepare method of your `Database` Class.. – Dipesh Parmar Sep 18 '13 at 09:42
  • ok so how can I get it to prepare method of the class I am in – user0129e021939232 Sep 18 '13 at 09:47
  • http://stackoverflow.com/questions/9722880/pdo-connection-class-code-and-class-design – Dipesh Parmar Sep 18 '13 at 09:48
  • @gutigrewal and for complete understanding read it here....http://www.sunnytuts.com/article/login-and-registration-with-object-oriented-php-and-pdo – Dipesh Parmar Sep 18 '13 at 09:51
  • thanks those links have helped me solve some of the errors will continue reading over this – user0129e021939232 Sep 18 '13 at 10:01
  • @gutigrewal glad i could helped you...let me know if you found any difficulties.. – Dipesh Parmar Sep 18 '13 at 10:06
1

Let me suggest that you rework the classes as follows.

class.Connect.php

<?php

// Database connection PDO

class Database {

    public function __construct($host, $dbname, $user, $pass) {
        // Attempt DB connection
        try
        {
            $this->pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);

class.Register.php

<?php

require 'class.Connect.php';

class Register {
        private $pdo;   // This variable will only be accessible from inside this class
    public function __construct($database)
         {

           $this->pdo = $database; //ofcourse you can get the db connections details and database name from a config file

And then use these classes like this.

// Connection information
$host   = 'localhost';
$dbname = 'imanage';
$user   = 'root';
$pass   = '';

$database = new Database($host, $dbname, $user, $pass);
$register = new Register($database);

This makes the classes a bit more portable and makes them easier to test.

Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109
0

In your code, the comment you added is:

class Register {
    public function __construct()
         {
           $this->pdo = new Database($host, $dbname, $user, $pass); 
           // ofcourse you can get the db connections details and database 
           // name from a config file
       }

But this is an object, so no, you actually can't. An object basically knows NOTHING about the outside world unless you specifically tell it so.

So, unless you want to use globals or some other ugly thing like that, you will want to either modify the way you create your new object:

$this->pdo = new Database();

You can do this as your database class already has the details in it (and secondly, your Register objects DOESN'T know about them, or you will have to declare them inside your Register object and modify the construct function of your database class to accept inputs.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
0

Your Database class doesn't have a "prepare" method.

Therefore this code: $stmt = $this->pdo->prepare($sql); Will result in the fatal error you are getting. $this->pdo only contains the instance of your Database class.

Fatal error: Call to undefined method Database::prepare() in E:\xampp\htdocs\attendance\class.Register.php on line 17

You can do $stmt = $this->pdo->pdo->prepare($sql); in your Register class. This will rid of the error you have. $this->pdo will be the instance of your Database class and then the property pdo in that class contains the instance of your pdo class.

n0tiz
  • 352
  • 1
  • 4