0

I've done a recipe_book with PDO, but not in an object oriented way. Now, I have to do it using objects.. (I do not have much experience with PHP in an object oriented way).

Basically, I am just trying to create my Connection class, but I guess I am doing something wrong, because I can not check that it has been successfully.

This is my Connection.php:

<?php

class Connection {

    protected $db_host;
    protected $db_user;
    protected $db_pass;
    protected $db_name;
    protected $db_driver;
    private $conn;

    function __construct() {

        $this->db_host = 'localhost';
        $this->db_user = 'someuser';
        $this->db_pass = 'somepass';
        $this->db_name = 'somedatabase';
        $this->db_driver = 'mysql';

        try {
            $this->conn = new PDO($this->db_driver . ":dbname=" . $this->db_name . ";host=" . $this->db_host, $this->db_user, $this->db_pass);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            echo 'Connected Successfully';
        } catch (PDOException $e) {
            echo "Connection failed: " . $e->getMessage();
        }

        $this->conn = $conn;
    }

//ends __construct 
}

I am not sure, what the __construct method should include as arguments.... and as much I look on other posts, more messed up it is in my mind. I can't see what I am doing wrong. What I am expecting, is, that is the connection is successful, if I access on the browser to this Connection.php file, it's too see echoed "Connected Successfully", but instead, I see nothing, just white page, and I don't know if I am doing something wrong.... or maybe I am expecting something wrong...

Could someone tell me how can I check that the connection has been created successfully?

Thank you

eve_mf
  • 795
  • 3
  • 12
  • 36
  • 2
    Useful reading: [Your first database wrapper's childhood diseases](https://phpdelusions.net/pdo/common_mistakes) – Your Common Sense Nov 16 '16 at 13:14
  • 3
    Where do you create the Connection class like `new Connection()` if you accsess Connection.php directly via browser? – JustOnUnderMillions Nov 16 '16 at 13:15
  • 1) Enable error reporting to see any errors, this is likely your main issue wrt. the "white page". 2) You don't need this class, it's useless. Just use a `PDO` instance directly. Create a *factory function* to instantiate it if you have to. – deceze Nov 16 '16 at 13:15
  • deceze, I have enabled error reporting but I am still seing the same "white page". I thought, the purpose of doing the project in an object oriented way included to include the connection as object too. So then, why do you say it is useless? Then...should I include the connection in the same way I did in the procedural way? Also, with all my respects, you marked it as duplicate, and I have looked ad the existing question and I don't see where it is duplicated :S JustOnUnderMillions, is it not what I am doing with $this->conn = new PDO and then with $this->conn = $conn ? – eve_mf Nov 16 '16 at 13:28
  • You're merely defining a class here. None of this code is executed until and unless you write `new Connection` somewhere outside of this class… – deceze Nov 16 '16 at 13:32
  • 1
    I was specifically referring to http://stackoverflow.com/a/12772851/476. And unless your `Connection` class adds any useful abilities, it is superfluous. If all it does is hold a PDO instance, you may just omit it and use the PDO instance directly. `PDO` already is a class and `new PDO` creates a new object from it. – deceze Nov 16 '16 at 13:34

0 Answers0