Home  >  Article  >  Backend Development  >  thinkphp配置连接数据库技巧_php实例

thinkphp配置连接数据库技巧_php实例

WBOY
WBOYOriginal
2016-05-16 20:29:18836browse

本文实例讲述了thinkphp配置连接数据库的常用方法,分享给大家供大家参考。具体方法如下:

1.在thinkphp入口文件同目录下的config.inc.php中添加数据库配置信息

复制代码 代码如下:
return array( 
'DB_TYPE'=>'mysqli', 
'DB_HOST'=>'localhost', 
'DB_NAME'=>'demo', 
'DB_USER'=>'root', 
'DB_PWD'=>'root', 
'DB_PREFIX'=>'think_', 
); 
?>

然后在项目目录的config.php文件中添加如下代码:
复制代码 代码如下:
$arr1=array( 
//'配置项'=>'配置值' 
'URL_MODEL'=>2, 
'APP_DEBUG'=>true, 
'LOG_RECORD'=>true, 
...... 
); 
$arr2=include 'config.inc.php'; 
return array_merge($arr1,$arr2); 
?>

在后台文件中也可以添加如上代码,这样在数据库配置的连接信息改动后,只需要修改config.inc.php文件中的信息就可以达到同时修改前台与后台的数据库连接配置信息。

2. 如果在控制器中要连接另外的数据库可以通过添加如下代码完成:

复制代码 代码如下:
$arrr=array( 
'dbms'=>'mysqli', //注意本书组中键名是规定的不能变动. 
'username'=>'google', 
'password'=>'hello', 
'hostname'=>'127.0.0.1', 
'database'=>'MYDBname', 
); 
$db=new Db($arrr);

希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn