Home > Article > PHP Framework > How to get database information in thinkphp
thinkphp obtains database information
Add database connection information to the project configuration file Conf/config.php:
// 添加数据库配置信息 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => 'thinkphp', // 数据库名 'DB_USER' => 'root', // 用户名 'DB_PWD' => '', // 密码 'DB_PORT' => 3306, // 端口 'DB_PREFIX' => 'think_', // 数据库表前缀
The main configuration items are the database server address hostname, database name database, database username username, database password password, and a table prefix prefix. After configuration, you can use tp5 query statements to query the database.
Or:
'DB_DSN' => 'mysql://root:密码@localhost:3306/thinkphp'
Modify the controller
Lib/Action/IndexAction.class.php
class IndexAction extends Action { public function index(){ $Data = M('tinyphp'); // 实例化Data数据模型,这行的tinyphp为数据表后缀名称 $this->data = $Data->select(); $this->display(); } }
Modify the template to allow data output
Tpl/Index/index.html
<html> <head> <title>Select Data</title> </head> <body> <volist name="data" id="vo"> {$vo.id}--{$vo.data}<br/> </volist> </body> </html>
The volist tag is the tag used by the built-in template engine to output the data set.
{$vo.id} and {$vo.data} are used similarly to Smarty. They are fields used to output data. Here, it means outputting the values of the id and data fields of the think_data table.
Recommended: php5 download
The above is the detailed content of How to get database information in thinkphp. For more information, please follow other related articles on the PHP Chinese website!