From e2c5b7c9129d45f887840a9cdb55c0d1a3912d59 Mon Sep 17 00:00:00 2001 From: "gary_chen\\gary_chen" Date: Thu, 9 Nov 2023 08:45:40 +0800 Subject: [PATCH] =?UTF-8?q?calander=20=E6=96=B0=E5=A2=9E=20.env=20?= =?UTF-8?q?=E9=80=A3=E7=B7=9A=E8=B3=87=E6=96=99=E5=BA=AB=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wms/frame/calendar/crud/connect.php | 32 +++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/wms/frame/calendar/crud/connect.php b/wms/frame/calendar/crud/connect.php index 4d83724d..ee661c44 100644 --- a/wms/frame/calendar/crud/connect.php +++ b/wms/frame/calendar/crud/connect.php @@ -1,8 +1,28 @@ exec("set names utf8"); +$envFile = __DIR__ . '/../.env'; // .env 文件的路徑 + +if (file_exists($envFile)) { + $lines = file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + if ($lines !== false) { + foreach ($lines as $line) { + list($key, $value) = explode('=', $line, 2); + $key = trim($key); + $value = trim($value); + // 设置环境变量 + putenv("$key=$value"); + } } - catch(PDOException $e) { - echo $e->getMessage(); - } \ No newline at end of file +} +date_default_timezone_set("Asia/Taipei"); +$host = getenv('DB_HOST'); +$dbuser = getenv('DB_USERNAME'); +$dbpassword = getenv('DB_PASSWORD'); +$dbname = getenv('DB_DATABASE'); + + +try { + $dbh = new PDO("mysql:host=$host:3306;dbname=$dbname", $dbuser, $dbpassword); + $dbh->exec("set names utf8"); +} catch (PDOException $e) { + echo $e->getMessage(); +}