23 thoughts on “PHP OOP Login/Register System: Database (Part 7/23)

  1. Working DB.php code for those who are getting errors as of 27th December, PHP 5.6:

    class DB
    {
    private static $_instance = null;
    private $_pdo,
    $_query,
    $_error = false,
    $_results,
    $_count = 0;

    private function __construct()
    {
    try {
    $this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host')
    . ';dbname=' . Config::get('mysql/db')
    , Config::get('mysql/username')
    , Config::get('mysql/password'));
    echo 'Connected';

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

    public static function getInstance() {
    if(!isset(self::$_instance)) {
    self::$_instance = new DB();
    }
    return self::$_instance;
    }
    }

  2. Thank you for your videos.I am following along and trying to copy and learn so that I can have a functioning website for my business. I am getting confused. When you are working on index.php after adding code later when you return to that file the code is missing. Then when I comment out the old code and try to use the last code I get errors while trying to test the connection as you did in this video. Here are my errors.any help would be much appreciated.

    Warning: require_once(core/init.php): failed to open stream: No such file or directory in C:xampphtdocsWebsiteIncludesindex.php on line 2

    Fatal error: require_once(): Failed opening required 'core/init.php' (include_path='C:xamppphpPEAR') in C:xampphtdocsWebsiteIncludesindex.php on line 2

  3. <?php
    /* database wrapper codebase reuseability PDO
    for the sole reason that we can then define the type of database we will be working with … MYsql
    */
    /* singelton pattern where you don't instantiate classes by new bd instated you call a get instance */

    require_once 'core/init.php';

    class DB {
    private static $_instance = null;
    private $_pdo, $_query, $_error = FALSE, $_results, $_Count = 0;

    private function __construct() {
    try {
    $this -> _pdo = new PDO('mysql:host=' . config::get('mysql/host') . ';dbname=' . config::get('mysql/database'), config::get('mysql/username'), config::get('mysql/password'));

    } catch(PDOExeption $e) {
    die($e -> getMessage());
    }
    }

    public static function getInstance() {
    if(!isset(self::$_instance)) {
    self::$_instance = new DB();
    }
    return self::$_instance;
    }

    public function query($sql, $params = array()){
    $this->_error = false;
    if($this->_query = $this->_pdo->prepare($sql)) {
    $x = 1;
    if(count($params)){
    foreach($params as $param){
    $this->_query->bindValue($x, $param);
    $x++;
    } }
    if($this->_query->execute()){
    echo 'Success';
    }
    }
    }

    }

    ?>

  4. Alex mentioned that the underscore is just a habit notation but not necessary to have correct? It is just a habit notation to refer to something that is private and not public?

    Also, in line 12, I thought he made a mistake because he didn't finish the fullstop after the semicolon… For example, he had this code in line 12:

    $this->_pdo = new PDO('mysql:host=' .Config::get('mysql/host') .'; dbname=' .Config::get('mysql/db'),Config::get('mysql/username'),Config::get('mysql/password'));
    } catch(PDOExeption $e) {

    and I thought it should be this:

    $this->_pdo = new PDO('mysql:host=' .Config::get('mysql/host') .'; dbname=' .Config::get('mysql/db').',Config::get('mysql/username').',Config::get('mysql/password'));
    } catch(PDOExeption $e) {

    It is from my understanding that you only need '. .' before the semicolon and not after? then why did he put the dot before config::get()? eg .Config:get()?

  5. Hello can any one explain to me how the private $_instance get filled when the constructor called I mean Alex have set the $this->$_pdo to the connection string and in the getInstance function he checked for $_instance if it were null or no ? confusing

  6. Dear Alex pls can you help me. I´m getting this Output in my Browser after the Test at minute 12:56 "could not find driver".I did everything like you the Testings as well. I´m using XAMPP and NETBEANS. Thank you in advance.

  7. please i have an issue here i tried using the config::get('mysql/db') i have being having this error( Parse error: syntax error, unexpected ',' ) i have tried checking is i mis-spell but no it all correct how can i resolve this

  8. 12:53 "Warning: PDO::__construct(): The server requested authentication method unknown to the client [mysql_old_password] in /home/*****/public_html/Log/classes/DB.php on line 12
    SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client"

    wtf

  9. If you want all these classes pre-typed for you, you can download any version of userspice version 3.1 or later to have them included and available for your own apps. Any version 3.5 or later will be fully built on these (slightly modified) classes.

  10. I am trying to do the example from 6:30 in my website, and it doesn't work, everything compiles, but nothing appears. From my understanding it doesn't make sense to ask for count() in the if statement then it should have a boolean?

    $users = DB::getInstance()->query('SELECT username FROM users');
    if($users->count()) {
    foreach ($users as $user) {
    echo $user->username;
    }
    }

Leave A Comment

Your email address will not be published. Required fields are marked *