1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Creating Database with PHP Constructor

Discussion in 'PHP' started by Gurbrinder, Apr 24, 2013.

  1. #1
    I am trying to create a Database by passing values to the class constructor.

    <?php
    class database
    {
    var $host;
    var $username;
    var $password;
     
    private function buildDB(){
    $sql = "create database cms";
    if(mysql_query($sql))
    {
    return mysql_query($sql);
    }
    else
    {
    echo mysql_error();
    }
    }
     
    public function __construct($host,$username,$password) {
        mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
        return $this->buildDB();
    }
     
     
    }
     
    $db_obj = new database('localhost','root','');
     
    ?>
    PHP:
    The above code returns Access denied for user ''@'localhost' to database 'cms', however the username , server and password is correct.

    :confused:
    Thanks
     
    Gurbrinder, Apr 24, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    You was trying to get values of the database object properties rather than the method variables you was passing.

    This is the code for accessing the object properties:

    
     
    <?php
    class database
    {
    var $host = 'localhost';
    var $username = 'root';
    var $password = '';
     
    private function buildDB(){
    $sql = "create database cms";
    if(mysql_query($sql))
    {
    return mysql_query($sql);
    }
    else
    {
    echo mysql_error();
    }
    }
     
    public function __construct() {
        mysql_connect($host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
        return $this->buildDB();
    }
     
     
    }
     
    $db_obj = new database();
    ?>
    
    PHP:

    This is the code for accessing a methods properties:

    
     
    <?php
    class database
    {
     
    private function buildDB(){
    $sql = "create database cms";
    if(mysql_query($sql))
    {
    return mysql_query($sql);
    }
    else
    {
    echo mysql_error();
    }
    }
     
    public function __construct($host, $username, $password) {
        mysql_connect($host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
        return $this->buildDB();
    }
     
     
    }
     
    $db_obj = new database('localhost', 'root', '');
    ?>
    
    PHP:
     
    HuggyStudios, Apr 25, 2013 IP
  3. Gurbrinder

    Gurbrinder Member

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Thanks for the help but second code is not working
    
    Notice: Undefined property: database::$username in C:\xampp\htdocs\ginny\cms\cms.php on line 18
     
    Notice: Undefined property: database::$password in C:\xampp\htdocs\ginny\cms\cms.php on line 18
    Access denied for user ''@'localhost' to database 'cms'
    Code (markup):
    correct code is
    <?php
    class database
    {
    private function buildDB(){
    $sql = "create database cms";
    if(mysql_query($sql))
    {
    return mysql_query($sql);
    }
    else
    {
    echo "error";
    }
    }
     
    public function __construct($host, $username, $password) {
        mysql_connect($host,$username,$password) or die("Could not connect. " . mysql_error());
        return $this->buildDB();
    }
     
     
    }
     
    $db_obj = new database('localhost', 'root', '');
    ?>
    PHP:
     
    Last edited: Apr 25, 2013
    Gurbrinder, Apr 25, 2013 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4

    Opps sorry, I had just got to work I was a bit heavy eyed. Glad you got it all working.
     
    HuggyStudios, Apr 25, 2013 IP