在不少项目中,配置文件是不可缺乏的,通常配置文件会用变量或者数组进行设置,例如数据库、或者一些公共参数。php
<?php // 配置文件数组 $config = array( 'DB_URL' => 'localhost', 'DB_USER' => 'root', 'DB_PWD' => 'root', 'DB_NAME' => 'test_db' ); // 代码格式 $str = '<?php'.PHP_EOL.'/**'.PHP_EOL.'* 配置文件'.PHP_EOL.'* TANKING'.PHP_EOL.'* '.date('Y-m-d').''.PHP_EOL.'*/'.PHP_EOL.'$db_config = '.var_export($config,true).';'.PHP_EOL.'?>'; // 建立文件 $file = fopen("config.php","w"); echo fwrite($file,$str); fclose($file); ?>
<?php /** * 配置文件 * TANKING * 2021-04-25 */ $db_config = array ( 'DB_URL' => 'localhost', 'DB_USER' => 'root', 'DB_PWD' => 'root', 'DB_NAME' => 'test_db', ); ?>
<?php // 引入数据库配置 include './config.php'; // 建立链接 $conn = mysqli_connect($db_config['DB_URL'], $db_config['DB_USER'], $db_config['DB_PWD']); // 检测链接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "链接成功"; ?>
Author:TANKING
DateL2021-04-25
WeChat:sansure2016
Web:http://www.likeyun.cnmysql