Mysql connection by Pdo does not exist

Asked

Viewed 100 times

-1

I can’t connect to my database, I have no errors, the connection just doesn’t happen, just gives me a blank page. I have these 4 small files responsible for Conn.

init.php:

<?php
session_start();

$GLOBALS['config'] = array(
    'mysql'         => array(
        'host'          => 'localhost',
        'username'      => 'root',
        'password'      => 'pass',
        'db'            => 'ooplr'
        ),
    'remember'      => array(
        'cookie_name'   => 'hash',
        'cookie_expiry' => 604800
        ),
    'session'       => array(
        'session_name'  => 'user'
        )
    );

spl_autoload_register(function($class) {
    require_once ('classes/' .$class. '.php');
});
require_once ('functions/sanitize.php');
?>

config.php:

<?php
class Config {
    public static function get($path = null) {
        if($path) {
            $config = $GLOBALS['config'];
            $path = explode('/', $path);

            foreach ($path as $bit) {
                if(isset($config[$bit])) {
                    $config = $config[$bit];
                }
            }
            return $config;
        }
        return false;
    }
}
?>

DB.php

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

    public 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'));
        } catch (PDOException $e) {
            die($e->getMessage());
        }
        echo ('connected');
    }
    public static function getInstance() {
        if(!isset(self::$_instance)) {
            self::$_instance = new DB();
        }
        return self::$_instance;
    }
}
?>

index php.

<?php
require_once('core/init.php');

DB::getInstance();
?>

1 answer

3


It seems that your constructor does not run as soon as the connection is not created, because the name is incorrect _construct() should be __construct()

constructors - manual

change:

public function _construct() {

for:

public function __construct() {
  • That’s what it was all about.

  • 1

    Just one more comment: all magic methods are prefixed with two underscores: _call, __set, __get, etc.

  • Okay understood, I didn’t know that obgado people

Browser other questions tagged

You are not signed in. Login or sign up in order to post.