SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post 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

    PHP Code:
    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
    PHP Code:
    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

    PHP Code:

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

      
    $user = new User();

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

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,411
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by jemz View Post
    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() "
    That's because your class Connection doesn't have a method called prepare, in fact it only has a __construct method:
    PHP Code:
    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:
    PHP Code:
    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.

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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);

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,411
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by jemz View Post
    Hi fretburner, Okay my connection.php content will be like this?
    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.

  5. #5
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    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

  6. #6
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,107
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Okay it's fine now


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •