PHP_THINKPHP_study3_数据库的配置和公共文件的读取

  • 一、配置读取数据库文件
  •      1、在Index/Conf/config.php 下
  •           编写数据库连接参数
  •          
    return array(
        //'配置项'=>'配置值'
        'DB_HOST' => 'localhost',
        'DB_USER' => 'hh',
        'DB_PWD' => '123456',
        'DB_NAME' => 'hh',
        'DB_PREFIX' => 'hh_'
    );
      
  •      在index()方法中测试
  •      
      public function index()
        {
            $db = M('user');
            $res = $db->select();
            dump($res);
        }
       输出结果:
  •  

     数据库里
  •  

     
  • 二、如果存在后台与前台调用一台服务器地址  
  •       1、在think 根目录下 新建Conf 文件下  新建一个 config.php
  •       

     2、在Admin 和 Index 的项目下的Conf/config.php 配置
  •        

           
    <?php
    $config = array(//'配置项'=>'配置值'
    );
    
    return array_merge(include './Conf/config.php', $config);
    
    ?>
        意思是将公用配置文件的数据配置 和自己的配置和成一个数组返回
  •     这样可以正常访问数据库了。
  •    ps: thinkphp 框架中可以参看  ThinkPHP/Conf/convention.php 中配置数据库的参数

猜你喜欢

转载自hefengjun1.iteye.com/blog/2332777
今日推荐