Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

database - is my cfg.php connection right?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...