0

The tutorial I am following online has me create two different PHP files. This is the Database.php file:

<?php
class dbConnection
{
    protected $db_connection;
    public $db_name = "todo";
    public $db_user = "root";
    public $db_password = "";
    public $db_host = "localhost";

    function connect()
    {
        try
        {
            $this -> db_connection = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this -> db_user, $this -> db_password);

            return $this -> db_connection;
        }

        catch(PDOException $e)
        {
            return $e -> getMessage();
        }
    }

}
?>

This is the ManageUsers.php file:

<?php
include_once ('Database.php');

class ManageUsers
{
    public $link;

    function __construct()
    {
        $dbConnection = new dbConnection();
        $this -> link = $dbConnection -> connect();

        return $this -> link;
    }

    function registerUsers($username, $password, $ip_address, $date, $time)
    {
        $query = $this->link->prepare('INSERT INTO users(username, password, ip_address, reg_date, reg_time) VALUES (?,?,?,?,?)');

        $values = array(
            $username,
            $password,
            $ip_address,
            $date,
            $time
        );

        $query -> execute($values);
        $rowCount = $query -> rowCount();

        return $rowCount;
    }

}

$users = new ManageUsers();

echo $users -> registerUsers("Bob", "Bob", "127.0.0.1", "07/08/2013", "9:34 A.M.");
?>

It keeps failing on the prepare(), and nothing in the tutorial tells me why. Any help would be greatly appreciated.

snowfi6916
  • 697
  • 4
  • 9
  • 21
  • Try changing return $e -> getMessage(); to echo $e -> getMessage(); and see if there's an error. – sker Jul 08 '13 at 14:45
  • The error is "Call to a member function prepare() on a non-object". – snowfi6916 Jul 08 '13 at 14:45
  • In the first place, stop following that tutorial. Secondly, it's clear that exception is thrown in `try` block in `connect` method, so it returns string representing message of `PDOException`. So in constructor, you assign string to `$this->link` that does not have any `prepare` method. – Leri Jul 08 '13 at 14:45
  • Also constructors should not return anything. – Leri Jul 08 '13 at 14:46
  • 1
    I echoed out the $e->getMessage()... it says "could not find driver". – snowfi6916 Jul 08 '13 at 14:48

2 Answers2

0

Probably, your database connection goes wrong and it returns a string instead of a PDO object:

$this -> db_connection = 
   new PDO("mysql:host=$this->db_host;dbname=$this->db_name", 
       $this -> db_user, $this -> db_password);

You have to write it a little different:

$this -> db_connection = 
   new PDO("mysql:host={$this->db_host};dbname={$this->db_name}", 
       $this -> db_user, $this -> db_password);

Also, make sure you correctly handle your errors (you would have found it by yourself if you had).

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
0

Your connect() function will either return the database object or the error string. You need to either actually throw the error inside of connect() without returning it, or check to see if $this->link is a string in __construct. Constructors shouldn't return a value anyway, or else you're assigning a value to the variable instead of the class instance.

aynber
  • 22,380
  • 8
  • 50
  • 63