is my cfg.php connection right?

2019-09-20 13:22发布

I have a quick question to you guys, i have created the CFG file which stores my database connection detail in two dimensional array. I then connect it to my PHP class file and make it launch the arrays stated in CFG file. As you can see below in my code:

cfg.php

 <?php
   $cfg['db']['host'] = 'localhost';
   $cfg['db']['user'] = 'root'; //your user name
   $cfg['db']['pass'] = ''; //your password 
   $cfg['db']['db'] = 'db3'; //your database
 ?>

and my class file :

 <?php

     require_once(dirname(__FILE__) . 'cfg.php');

class Database {

    private $dbConn; //stores the database connection

   public function __construct($dbConn)
     {
    global $cfg;
    mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die('Could not connect to MySQL server.');
    mysql_select_db(DB_DATABASE)or die('Unable to select database: ');
    }



}

What i want to ask you is: is this right way of doing this? also what do I need to add in my index to see that it is connected. and the output of the database content. Thank you in advance for taking time and reading my problem. Cheerio.

Edit :

      <?php

        require_once(dirname(__FILE__) . 'cfg.php');

  class Database {

     private $dbConn; //stores the database connection

public function __construct($dbConn)
{
    global $cfg;
    mysqli_connect($cfg['db']['host'], $cfg['db']['user'], $cfg['db']['pass'])
    or die('Could not connect to MySQL server.');
    mysqli_select_db($dbConn, $cfg['db']['db'])
    or die('Unable to select database: ');
}


}

Does this looks better now? If yes. How do i connect it with the index.php file where my forms will be stored. say to output the message of (connected to database). Thank you.

EDIT: changed to mysqli and now when selecting the database it states that i am missing the database name. Not sure where to put that and how to alter it. Thank you.

EDIT: I am on my way to create functions for 'Select' 'Insert' and 'Delete' . If any of you can point me do a great source of information which will help me in my research it will be most appreciated.

标签: php database oop
2条回答
放荡不羁爱自由
2楼-- · 2019-09-20 14:13

You are using constants instead of the actual values from your config in your mysql_connect() function, so that wouldn't work. You would need to do it this way:

mysql_connect($cfg['db']['host'], $cfg['db']['user'], $cfg['db']['pass'])

Aside from that and OO paradigms, it would probably be better if you used PHP's mysqli (as stated here) or PDO, as PHP's mysql_ is pretty outdated.

查看更多
叼着烟拽天下
3楼-- · 2019-09-20 14:15

No you are not doing it correctly. By using this line

require_once(dirname(__FILE__) . 'cfg.php');

in your class file you have introduced a somewhat tight coupling to the config file and you have made it hard to swap it out for other credentials. I.e. you will have to change the file with the Database class in it to change the credentials. So start by dropping that include there.

Another thing you do is using the global keyword which is as far from OOP as you could possibly get. Inject the information the class needs instead.

Also you are using the ancient, deprecated and soon to be removed mysql_* API. You are also calling the execution of your script of something fails (or die()) which makes it impossible to integrate your code in a project and also makes it impossible to correctly handle errors (i.e. custom error page).

When upgrading to a better database API (e.g. PDO) you even don't have the need anymore to use a database class at all.

The above would simply look something like the following:

bootstrap.php

try {
    $dbConnection = new \PDO('mysql:dbname=dbtest;host=127.0.0.1;charset=utf8', 'user', 'pass');

    $dbConnection->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
    $dbConnection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
} catch (\PDOException $e) {
    // nicely handle error here
}

Alternatively you could implement lazy loading for the database connection depending on your requirements. As you can see now there is no need for an external config file (you can just change the credentials here in the bootstrap file) and there is no need for a database class.

查看更多
登录 后发表回答