0

Hi i'm making a login and register script. I hava global array for my Config. But when i'm trying to make connection to my database. He can't get it from the array only the host. He won't go deeper inside it. Only thing i get is 127.0.0.1 for each thing i want to have so like i want to have the username i will get 127.0.0.1. I have no idea whats wrong but i think somethings wrong in my Config.php. Also this is my output on my screen. Someone know what i'm doing wrong ?

I only get the host array back. And when i want the username or db name back i get the host. enter image description here Here is al my code.

index.php

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


DB::getInstance();

Init.php

    <?php
session_start();

$GLOBALS['config'] = array(
    'mysql' => array(
                    'host'      => '127.0.0.1',
                    'username'  => 'root',
                    'password'  => '',
                    'db'        => 'login'

                    ),
    'remember' => array(
                    'cookie_name'   => 'hash',
                    'cookie_expiry' => '648000'
    ),

    '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'];
        foreach($config as $key =>$value){
            if(isset($value)){
                $config1 = $value;
                foreach ($config1 as $key =>$list){
                    return  $list;
                }
            }

        }
          return false;
        }    

    }
}

DB.php

<?php

class DB{
    private static $_instance = null;
    private $_pdo, $_query, $_error = false, $_result, $_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'));
        }catch(PDOException $e){
            echo Config::get('mysql/host') . Config::get('mysql/db') . Config::get('mysql/db'); // Only for testing getting 127.0.0.1 back for each of them.

        }
    }


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

2 Answers 2

5
private static $_istance = null;

Tyyyypoooooooooooo :) n missing

Edit

Typoooooooooo number 2

'msql' => array(

Should by mysql, that is why your config doesnt load.

Sign up to request clarification or add additional context in comments.

4 Comments

O yes i see, changed it bull still have the same problem.
Still the same one as i put above. Acces denied. He is giving my username and Password db name 127.0.0.1 from the array.
Access Denied is a different error, most probably your database credentials are not correct. If you have fixed the typo in variable declaration you cant be getting the same error message again. Can you please post the new exact error message.
Yes i see it. Only problem is that its still not working why i only get the first array back called host that 127.0.0.1 And not like my username db name.
0

After a rewrite of my Config.php class it worked.

<?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;
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.