是我的cfg.php连接权? [英] is my cfg.php connection right?

查看:134
本文介绍了是我的cfg.php连接权?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个快速的问题给你们,我创建了CFG文件,其中存储我的数据库连接细节在二维数组。然后将它连接到我的PHP类文件,并使其启动CFG文件中声明的数组。如下面的代码所示:

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

和我的类文件:

 <?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: ');
    }



}

我想问你是:这是正确的方式这样做吗?还有什么我需要添加在我的索引,看看它是连接。和数据库内容的输出。预先感谢你花时间和阅读我的问题。 Cheerio。

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.

编辑:

      <?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: ');
}


}

现在好多了?如是。如何将其与我的表单将被存储的index.php文件连接。说输出(连接到数据库)的消息。谢谢。

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.

编辑:更改为mysqli,现在选择数据库时,它指出我缺少数据库名称。不知道放在哪里,如何改变它。谢谢。

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.

编辑:我正在为选择插入和删除创建函数。

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.

推荐答案

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

在你的类文件中,你引入了一个有点紧耦合到配置文件,将其交换为其他凭据。也就是说您将必须更改其中的Database类的文件以更改凭据。

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.

另一件事情是使用 全局关键字,远离OOP。

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.

此外,您使用的是古老的,已过时且很快被删除 mysql _ * API。你也调用你的脚本的执行失败(或die()),这使得它不可能集成你的代码在一个项目,也使得不可能正确处理错误(即自定义错误页面)。

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).

当升级到更好的数据库API(例如PDO)时,你甚至不需要再使用数据库类。

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.

这篇关于是我的cfg.php连接权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆