我有一些连接到DataBase的代码。登录信息困扰我,因为每次我想在函数或其他东西中调用我的登录信息时,我必须输入至少3个变量。是否有可能制作一个包含所有登录信息的变量?
<?php
// db login information
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mYcms";
// db connection
class dbconnect
{
function __construct($servername, $username, $password)
{
if (!@$this->connect($servername, $username, $password)) {
echo "connection failed";
} else {
echo "connected to " . $servername;
}
}
function connect($servername, $username, $password){
if (!mysql_connect($servername, $username, $password)) {
return false;
} else {
return true;
}
}
}
$connection = new dbconnect($servername, $username, $password);
?>
答案 0 :(得分:0)
您可以使用数组:
$connection = array();
$connection['servername'] = "name of server";
$connection['username'] = "user";
$connection['password'] = "password";
$connection['dbname'] = "name of DB";
$connection = new dbconnect($connection);
你班上的:
class dbconnect {
// db login information
var $connection;
function __construct($connection){
$this->servername = $connection['servername'];
$this->username = $connection['username'];
$this->password = $connection['password'];
$this->dbname = $connection['dbname'];
ecc...
我希望你有所帮助