0

I am new to OOP and PHP, so I have a problem here. Could someone tell me, what I am doing wrong with my connection class? It does not connecting to the database, I've tried to var_dump($this) in try statement and it either doesn't work. Also I am changing my 'dbname' to a random name and the code still 'works'..

Here is my code:

<?php
    class connection {
        // Setting Database Source Name (DSN)
        public function __construct() {
            $dsn = 'mysql:host=localhost;dbname=employee';
            // Setting options
            $options = array (PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
            // Making the connection to the database
            try {
                $this->dbh = new PDO($dsn, 'root', '', $options); 
            }
            catch (PDOException $e) {
                $this->error = $e->getMessage();
            }
        }
    }
    $connection = new connection();
?>
MrDarkLynx
  • 686
  • 1
  • 9
  • 15
HELPME
  • 714
  • 14
  • 35
  • 1
    Try `echo $e->getMessage();` in the `catch` to see the error details. It works because of the try catch block – codtex Mar 06 '17 at 09:31
  • You should enable Displaying errors first. Your class properties are not defined yet. – Raptor Mar 06 '17 at 09:31
  • and yes put on top of script `error_reporting(E_ALL);` and `ini_set("display_errors", 1);` – codtex Mar 06 '17 at 09:32
  • @sand thank you, it helped, there was wrong database name... Could someone tell me if my code structure is good or not? – HELPME Mar 06 '17 at 09:35
  • @YourCommonSense Doesn't it has to be 'single responsible principle'? I.e 'connection' is in 1 class, inserting values to the database is in another file class and etc? Besides that, I've tried your code, which you've posted in my previous question, but it didn't worked, so I'm trying to write my own from beginning according to your code. – HELPME Mar 06 '17 at 09:41
  • You understands this principle quite perverted way. thinking like this, there will be only classes with single method only. If you want to try my code, then try it with vanilla PDO. – Your Common Sense Mar 06 '17 at 09:54
  • @YourCommonSense that's not a duplicate of this question that you marked this with. here we the Op is having a problem with a connection class, – Masivuye Cokile Mar 06 '17 at 09:59
  • this would been better http://stackoverflow.com/questions/13240500/creating-a-database-connection-class-pdo-and-fetch-data – Masivuye Cokile Mar 06 '17 at 10:04
  • Or this : http://stackoverflow.com/questions/15864856/pdo-connection-class – Masivuye Cokile Mar 06 '17 at 10:04

1 Answers1

0

Every method in class has to return something. Try:

  class Connection {

   private $connection;    
    // Setting Database Source Name (DSN)
    public function __construct() {
        $dsn = 'mysql:host=localhost;dbname=employee';
        // Setting options
        $options = array (PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
        // Making the connection to the database
        try {
            $this->connection = new PDO($dsn, 'root', '', $options); 
        }
        catch (PDOException $e) {
            $this->error = $e->getMessage();
        }
    }

    public function DoSomething()
    {
         //do something with your $this->connection and return some value;
    }
}
$connection = new Connection();
echo $connection->DoSomething();
xxx
  • 9
  • 1