您不要将信息放入文件中,而是在声明类时添加它们
解释
该类包含
/**
* Constructor. Initializes a database connection and selects our database.
* @param string $host The host to wchich to connect.
* @param string $username The name of the user used to login to the database.
* @param string $password The password of the user to login to the database.
* @param string $database The name of the database to which to connect.
*/
function __construct($host, $username, $password, $database)
{
$this->DB_HOST = $host;
$this->DB_USERNAME = $username;
$this->DB_PASSWORD = $password;
$this->DB_DATABASE = $database;
}
你在 PHP 代码中需要的是这样的
$db = new DbConnect("localhost","username","password","database");
这是一个bad practice
尝试直接在类中硬编码值,如果您需要连接到多个host
或database
乔·伯内特
作为一个初学者,当 MySQL 作为出色的 `mysqli' 扩展时,你不需要这样的类,它也是面向对象的并且非常快。
用法与您没有的类似,但不需要任何额外的内容,包括任何类
例子
$db = new mysqli("localhost","username","password","database");
有关更多信息和示例,请参见http://www.php.net/manual/en/mysqli.construct.php