SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 33

Thread: Am I inscure?

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking Am I inscure?

    Hi guys,

    Ok... erm

    Right...

    Im building a base class for my website that is going to contain all the basic information for my site, like the header, footer, mysql connection (which is going to be in a separate class).

    The mysql connection is what I'm unsure about, If I set the connection variables (ie. mysql username, password, host, etc) to private and made the actual connector ($connect = mysql_connect(xxxxx)) public, would that be secure of not? It's just that its getting a bit tedious having to make mysql connections for each class (i mean having to call my mysql class).

    To put it short, I want to make a connection to my mysql server in the class "base" and for all the classes that extend the "base" class to be be able to access the connection without having to make a new one, would setting the variables to private and the connector to public be secure or not?

    Thanks.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Have your base class request an 'instance' of your database connection, you should not really have to connect in every child class.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Something similar to:-

    PHP Code:
    <?php

    class Database
    {
        private static 
    $oInstance null;
        
        public static function 
    getInstance()
        {
            if(
    is_null(self::$oInstance))
            {
                
    self::$oInstance = new Database'localhost''username''password''database' );
            }
            return 
    self::$oInstance;
        }
        
        private function 
    __construct$sHost$sUsername$sPassword$sDatabase$iPort 3306 )
        {
            try{
                
    self::$oInstance = new mysqli$sHost$sUsername$sPassword$sDatabase$iPort );
                return;
            }catch(
    Exception $oException){
                throw new 
    Exception($oException->getMessage());
                return;
            }
        }
    }

    class 
    CMS
    {
        protected 
    $oDatabase;
        
        public function 
    __construct()
        {
            
    $this->oDatabase Database::getInstance();
        }
    }

    class 
    Users extends CMS
    {
        public function 
    getUserByID$iID )
        {
            
    $oResult $this->oDatabase->query("SELECT name,email FROM users WHERE id = $iID LIMIT 1;");
            
    // Do Stuff.
        
    }
    }

    ?>
    I hope I'm making sense, or should I say, talking sense!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, That makes sense.

  5. #5
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Great, just give me a shout if you stuck.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do i pass to the database class?

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    How do you mean?

    You don't have to pass the database class to any other anymore, the getInstance() method will automatically create/return it for you.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually it doesn't matter, i've come realised that I don't need to pass to the database class.

  9. #9
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is mysqli different to mysql?

  10. #10
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by L4DD13 View Post
    Is mysqli different to mysql?
    It is. It's still a wrapper to MySQL, however, so you can use either one. The i stands for improved. It also comes in OO flavour. Recommended over the mysql_* functions.

  11. #11
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im lame i know, but I cant seem to get the script to do querys

  12. #12
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by L4DD13 View Post
    im lame i know, but I cant seem to get the script to do querys
    You're not lame. You're learning.

    http://uk.php.net/manual/en/class.mysqli.php

  13. #13
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by decowski View Post
    You're not lame. You're learning.

    http://uk.php.net/manual/en/class.mysqli.php
    Thanks


    I cant still cant get a query to work, I have the following:

    How would i get a query to work with the class you provided?
    Do i have to add to the class before i can make a query?

  14. #14
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, SilverBulletUK has already posted the code you need to use. What problem exactly are you having with that code?

  15. #15
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get errors,

    When i use: $settings_query = mysqli_query( $this->_oDatabase , "SELECT * FROM `"._settingstbl."");
    I get: mysqli_query() expects parameter 1 to be mysqli, null given in C:\xampp\htdocs\classes\class.base.php on line 22

    And,
    When I use: $get_settings = $this->_oDatabase->query("SELECT * FROM `"._settingstbl."`");
    I get: Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\classes\class.base.php on line 21

  16. #16
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should use the latter.

    Quote Originally Posted by L4DD13 View Post
    When I use: $get_settings = $this->_oDatabase->query("SELECT * FROM `"._settingstbl."`");
    I get: Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\classes\class.base.php on line 21
    Now this means that the $_oDatabase property is not a properly instantiated object. In his example, SilverBulletUK used $oDatabase (without the underscore). Make sure you're using the same property names.

  17. #17
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the script:

    PHP Code:
    <?php

    class database
    {

        private static 
    $oInstance null;
        public 
    $_config;

        public static function 
    getInstance()
        {
            if(
    is_null(self::$oInstance))
            {
                require_once(
    "../cwp-config.php");
                
    self::$oInstance = new DatabaseDBHOSTDBUSERNAMEDBPASSWORDDEFAULT_DATABASE );
            }
            return 
    self::$oInstance;
        }

        private function 
    __construct($sHost$sUsername$sPassword$sDatabase$iPort 3306)
        {
            try{
                
    self::$oInstance = new mysqli$sHost$sUsername$sPassword$sDatabase$iPort );
                return;
            }catch(
    Exception $oException){
                throw new 
    Exception($oException->getMessage());
                return;
            }

        }

    }

    class 
    base
    {

        protected     
    $oDatabase;// Database connection
        
    public         $_filebase;    // The absolute path to the website on the server.
        
    public        $_urlbase;     // The base url of the website.
        
    public        $_config;     // Location of the config.

        
    public function __construct()
        {
            
            
    $this->oDatabase Database::getInstance();

            
    $this->_filebase            $_SERVER['DOCUMENT_ROOT'] . "/";
            
    $this->_urlbase                "http://" $_SERVER['HTTP_HOST'];
            
    $this->_config                "../cwp-config.php";

            require_once(
    $this->_config);

            
    // Database Connection
            
    $get_settings $this->oDatabase->query("SELECT * FROM `"._settingstbl."`");
            if(
    mysqli_num_rows($get_settings))
            {
                echo 
    'settings';
            } else {
                echo 
    'no settings';
            }

        }

        public function 
    _header()
        {}

        public function 
    _footer()
        {}

        public function 
    _urlbase(){ return $this->_urlbase; }

    }

    ?>

  18. #18
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Try this modified, Database class...

    PHP Code:
    class Database
    {
        private static 
    $oInstance null;
        
        public static function 
    getInstance()
        {
            if(
    is_null(self::$oInstance))
            {
                
    self::$oInstance = new Database'localhost''username''password''database' );
            }
            return 
    self::$oInstance;
        }
        
        private function 
    __construct$sHost$sUsername$sPassword$sDatabase$iPort 3306 )
        {
            try{
                
    self::$oInstance = new mysqli$sHost$sUsername$sPassword$sDatabase$iPort );
                if( ! 
    is_object(self::$oInstance) || self::$oInstance->connect_error )
                {
                    throw new 
    Exception(mysqli_connect_error());
                }
                return;
            }catch(
    Exception $oException){
                throw new 
    Exception($oException->getMessage());
                return;
            }
        }

    I'm not entirely sure if the mysqli object throws an exception when it doesn't connect, so I just added a little routine.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  19. #19
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In Database constructor check if the object has been created before returning it. If it hasn't been created then check for errors. You're probably passing wrong database details to the function (host, username, password).

    Also at the top of your script add:

    PHP Code:
    error_reporting(E_ALL E_STRICT); 

  20. #20
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Try this modified, Database class...

    PHP Code:
    class Database
    {
        private static 
    $oInstance null;
        
        public static function 
    getInstance()
        {
            if(
    is_null(self::$oInstance))
            {
                
    self::$oInstance = new Database'localhost''username''password''database' );
            }
            return 
    self::$oInstance;
        }
        
        private function 
    __construct$sHost$sUsername$sPassword$sDatabase$iPort 3306 )
        {
            try{
                
    self::$oInstance = new mysqli$sHost$sUsername$sPassword$sDatabase$iPort );
                if( ! 
    is_object(self::$oInstance) || self::$oInstance->connect_error )
                {
                    throw new 
    Exception(mysqli_connect_error());
                }
                return;
            }catch(
    Exception $oException){
                throw new 
    Exception($oException->getMessage());
                return;
            }
        }

    I'm not entirely sure if the mysqli object throws an exception when it doesn't connect, so I just added a little routine.
    Quote Originally Posted by decowski View Post
    In Database constructor check if the object has been created before returning it. If it hasn't been created then check for errors. You're probably passing wrong database details to the function (host, username, password).

    Also at the top of your script add:

    PHP Code:
    error_reporting(E_ALL E_STRICT); 
    After implementing both of your suggestions i get the following error:
    Fatal error: Call to undefined method Database::query() in C:\xampp\htdocs\classes\class.base.php on line 86

  21. #21
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    Quote Originally Posted by L4DD13 View Post
    PHP Code:
        public static function getInstance()
        {
            if(
    is_null(self::$oInstance)) {
                
    self::$oInstance = new Database'localhost''username''password''database' );
                }

            return 
    self::$oInstance;
        } 
    You're assigning self::$oInstance an instance of Database, not mysqli. You can solve it like this:

    PHP Code:
        public static function getInstance()
        {
            if(
    is_null(self::$oInstance)) {
                new 
    Database'localhost''username''password''database' );
                }

            return 
    self::$oInstance;
        } 
    But it's a convoluted way of doing it. There's no need to instantiate a Database object so there shouldn't be a constructor at all.

  22. #22
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah ha THANKS GUYS,

    Its working now, much appreciated.

  23. #23
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Doh!

    My bad, sorry! That's what I get for quickly knocking up code, then just assuming it's correct...apologies.

    Thanks Decowski.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  24. #24
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One last thing...

    Using error_reporting(E_ALL | E_STRICT); is stopping me from using the likes of mktime().
    Is there anyway around it?

  25. #25
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by L4DD13 View Post
    One last thing...

    Using error_reporting(E_ALL | E_STRICT); is stopping me from using the likes of mktime().
    Is there anyway around it?
    How is it stopping you?


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
  •