初始化
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
// 加载 .env 文件中的环境变量
|
||||
$envFile = __DIR__ . '/../.env';
|
||||
if (file_exists($envFile) && is_readable($envFile)) {
|
||||
$lines = @file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
if ($lines !== false) {
|
||||
foreach ($lines as $line) {
|
||||
$line = trim($line);
|
||||
if ($line === '' || $line[0] === '#') continue;
|
||||
if (strpos($line, '=') !== false) {
|
||||
list($key, $value) = explode('=', $line, 2);
|
||||
$key = trim($key);
|
||||
$value = trim($value);
|
||||
// 去除引号包裹
|
||||
if ((str_starts_with($value, '"') && str_ends_with($value, '"')) ||
|
||||
(str_starts_with($value, "'") && str_ends_with($value, "'"))) {
|
||||
$value = substr($value, 1, -1);
|
||||
}
|
||||
if (!array_key_exists($key, $_ENV)) {
|
||||
$_ENV[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$config['db']['host'] = $_ENV['DB_HOST'] ?? 'localhost';
|
||||
$config['db']['username'] = $_ENV['DB_USER'] ?? 'root';
|
||||
$config['db']['password'] = $_ENV['DB_PASS'] ?? '';
|
||||
$config['db']['dbname'] = $_ENV['DB_NAME'] ?? 'mes2';
|
||||
$config['db']['company'] = $_ENV['DB_COMPANY'] ?? 'Default Company';
|
||||
$config['defaultAction'] = 'index';
|
||||
return $config;
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
// 兼容性数据库连接,实际由 core/db/Db.php 通过 config.php 驱动
|
||||
// 从 .env 文件读取配置
|
||||
$envFile = __DIR__ . '/../.env';
|
||||
$dbHost = 'localhost';
|
||||
$dbUser = 'root';
|
||||
$dbPass = '';
|
||||
$dbName = 'mes2';
|
||||
|
||||
if (file_exists($envFile)) {
|
||||
$lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
foreach ($lines as $line) {
|
||||
$line = trim($line);
|
||||
if ($line === '' || $line[0] === '#') continue;
|
||||
if (strpos($line, '=') !== false) {
|
||||
list($key, $value) = explode('=', $line, 2);
|
||||
$key = trim($key);
|
||||
$value = trim($value);
|
||||
// 去除引号包裹
|
||||
if ((str_starts_with($value, '"') && str_ends_with($value, '"')) ||
|
||||
(str_starts_with($value, "'") && str_ends_with($value, "'"))) {
|
||||
$value = substr($value, 1, -1);
|
||||
}
|
||||
switch ($key) {
|
||||
case 'DB_HOST': $dbHost = $value; break;
|
||||
case 'DB_USER': $dbUser = $value; break;
|
||||
case 'DB_PASS': $dbPass = $value; break;
|
||||
case 'DB_NAME': $dbName = $value; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
|
||||
if ($conn->connect_error) {
|
||||
error_log('数据库连接失败:' . $conn->connect_error);
|
||||
die('数据库连接失败,请联系管理员');
|
||||
}
|
||||
$conn->set_charset("utf8mb4");
|
||||
Reference in New Issue
Block a user