-2

I know this is might be a duplicate post, however all of the examples didn't quite help me, examples like this im trying to pass my db connection to user class.

What am i doing wrong

i get the follow errors

 Undefined property: Db::$connect 

 Call to a member function prepare() on a non-object

Db.php

<?php 

error_reporting(-1);

class Db{

    private $db_host;
    private $db_user;
    private $db_name;
    private $db_pass;

    public function connect()
    {
        $this->db_host = "127.0.0.1";
        $this->db_user = "root";
        $this->db_pass = "root";
        $this->db_name = "eli9";

        try {
            $db = new PDO("mysql:host=127.0.0.1;port=8889,dbname=eli9", 'root', 'root');
            $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
            echo "connected \n";


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


        return $db;

    }


}

User.php

<?php

require_once 'Db.php';

class User extends Db{

    private $db;

    public function __construct()
    {
        $this->db = new Db();

    }


    public function signup($email, $password, $username)
    {
        try{
            $stmt = $this->db->connect->prepare("INSERT INTO users (user_email, user_pass, user_name) VALUES (:email, :password, :username) ");

            $stmt->bindparam(':email', $email);
            $stmt->bindparam(':password', $password);
            $stmt->bindparam(':username', $username);
            $stmt->execute();

        }

        catch(PDOExeception $e)
        {
            echo $e->getMessage();
        }


    }


}
BARNOWL
  • 3,326
  • 10
  • 44
  • 80
  • This might help: https://stackoverflow.com/a/11369679/727208 – tereško May 27 '17 at 23:57
  • You have a typo, you should be doing `$this->db->connect()->` not `$this->db->connect->`. Though you can do what you are doing in your construct, it's somewhat pointless because the answer below says, `User` has inherited the methods of `Db` – Rasclatt May 28 '17 at 01:11
  • @Rasclatt thanks i understand now, thanks man – BARNOWL May 28 '17 at 01:40

1 Answers1

2

I might be wrong but I don't think you can create your user class extending your DB class and then use the DB class connection function in your constructor function.

Your user class should have inherited your DB connection which you should be able to use like

$this->db = $this->connect();

in your constructor function.

abetwothree
  • 575
  • 2
  • 8
  • 28
  • it works however, now i get this `PDOStatement::execute(): SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected in /Applications/MAMP/htdocs/eli14/User.php on line 24` – BARNOWL May 28 '17 at 01:43
  • 1
    Try changing this `mysql:host=127.0.0.1;port=8889,dbname=eli9` to this `mysql:host=127.0.0.1;dbname=eli9`. Not sure if you really need the port. – abetwothree May 28 '17 at 02:04
  • 1
    wierd, i just did that before you made that comment. Thanks abraham anyway. it works now. – BARNOWL May 28 '17 at 02:10