Reputation: 127
i want to know what is the following ways are better programming way and why :
1-just use a one file for connection :
connection.php:
<?php
$connection=mysql_connect("localhost","root","");
if(!$connection)
{
die("data base connection faild:".mysql_error());
}
$db=mysql_select_db("widget_corp");
if(!$db)
{
die("not db selection".mysql_error());
}
?>
2- use a file for connection and an other to store db access information:
connection.php:
<?php
require_once 'constants.php';
$connection=mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if(!$connection)
{
die("data base connection faild:".mysql_error());
}
$db=mysql_select_db(DB_NAME);
if(!$db)
{
die("not db selection".mysql_error());
}
?>
constans.php
<?php
define('DB_SERVER','localhost');
define('DB_USER', 'root');
define('DB_PASS','');
define('DB_NAME','widget_corp');
?>
Upvotes: 1
Views: 64
Reputation: 3526
I prefer use this method
return array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
);
and then
$config = include 'config.php';
$connection=mysql_connect($config['host'],['username'],$config['password']);
Upvotes: 1