我正在为我的社区制作一个基于MySQL和PHP的控制面板。而且我已经用带有配置的数组制作了一个settings.php文件。我有函数的类文件,但有MySQL函数,该函数不会与我在settings.php上输入的数据连接。.
我尝试了其他配置文件选项,但是它们都不起作用...
在settings.php上,我有这样的MySQL数据:
$config['database']['host'] = "---";
$config['database']['user'] = "---";
$config['database']['password'] = "---";
$config['database']['database'] = "---";
在userdata.php类文件上,我尝试使用这些配置变量:
$mysql = new mysqli($config['database']['host'], $config['database']['user'], $config['database']['password'], $config['database']['database']);
显然,在userdata.php上,我还需要settings.php。
我期望输出是正确的,但是它只显示“错误的MySQL数据”错误...
答案 0 :(得分:0)
您可以在connection.php
文件中尝试这种方式
<?php
include_once("path/config.php");
function OpenCon()
{
$dbhost = $config['database']['host'];
$dbuser = $config['database']['user'];
$dbpass = $config['database']['password'];
$db = $config['database']['db'];
$conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s\n".
$conn -> error);
return $conn;
}
答案 1 :(得分:0)
我可以使用settings.php文件,例如:
$config = array (
'db_host' => 'xxx',
'db_user' => 'xxx',
'db_pass' => 'xxx',
'db_database' => 'xxx'
和userdata.php一样:
include 'settings.php';
$host = $config['db_host'];
$user = $config['db_user'];
$pass = $config['db_pass'];
$database = $config['db_database'];
$mysql = new mysqli($host, $user, $pass, $database);