Why i could not connect on this

Hi, I am having problem on this, i am creating class connection and I am trying to create simple registration but it says"Call to undefined method Connection prepare() "

Connection.php


class Connection {

   public  function __construct(){
       try{
           $connection= new PDO('mysql:localhost;dbname=mydb','root','');
           $connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);


       }
       catch(PDOException $error){
           die ($error->getMessage());
       }
     }



}

Genpage.php


class User {

    public function __construct(){


    }


    public function Register($username,$password){
        $dbconn = new Connection();

        $query = $dbconn->prepare('INSERT into tbluser (username,password) values(?,?)');
        $query ->execute(array($username,$password));


    }

}

index.php



  include_once "User.php";
  include_once "Connection.php";

  $user = new User();

  $user->Register("jemz","pwd123");





That’s because your class Connection doesn’t have a method called prepare, in fact it only has a __construct method:


class Connection {

   public  function __construct(){
       try{
           $connection= new PDO('mysql:localhost;dbname=mydb','root','');
           $connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
       }
       catch(PDOException $error){
           die ($error->getMessage());
       }
     }

}

The $connection variable you’re creating in the constructor isn’t available outside of the constructor.

Have a look at this example:


class User
{
    protected $connection;

    public function __construct(PDO $connection)
    {
        $this->connection = $connection;
    }

    public function register($username, $password)
    {
        $query = $this->connection->prepare('INSERT into tbluser (username,password) values(?,?)');
        $query->execute(array($username,$password));
    }
}

// Usage
$connection = new PDO('mysql:localhost;dbname=mydb','root','');
$connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

$user = new User($connection);

Here the User class has a property called $connection which is marked as protected, meaning it’s not accessible from outside the object itself, but it can be accessed from any method in User as $this->connection.

Notice that I’m passing in the connection object via a contructor argument, rather than creating it there. This is good practice for several reasons, one of which is that I only have to instantate one connection object in the app and I can pass it to all the other objects that need it. In your example, you’re creating a new connection everytime you create a new user.

Hi fretburner, Okay my connection.php content will be like this?

// Usage
$connection = new PDO(‘mysql:localhost;dbname=mydb’,‘root’,‘’);
$connection->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

Sure, you can do it that way, it really depends on how you’re laying out your app. These days the front controller pattern is popular, where you set up your webserver to route all requests through a single script (index.php or whatever) and you’d do all your app configuration (like setting up a DB connection) there.

Thank you :slight_smile:

Okay it’s fine now :slight_smile: