Home > PHP Framework > ThinkPHP > How to connect thinkphp to the database

How to connect thinkphp to the database

爱喝马黛茶的安东尼
Release: 2019-08-22 10:53:40
Original
4371 people have browsed it

How to connect thinkphp to the database

First open Zend to create a project and import our ThinkPHP.

How to connect thinkphp to the database

Enter http://1270.0.1/text01 in the browser address bar and run the ThinkPHP project, you can see Think’s smiling face.

How to connect thinkphp to the database

Related recommendations: "ThinkPHP Tutorial"

Create a database Create a user table for the database connection of our entire project , test whether the database is connected.

How to connect thinkphp to the database

Open the project refresh and view the generated directory structure. Application->Common->Conf is the public configuration file directory of the project. You can see that there is a config under Conf. .php file, "Public Configuration File Directory" can be seen that this directory is a common directory for the front and backend. If the front and backend use a database to directly configure a database connection in the public directory, it can be used by both the front and backend. It can be used under config.php Write all public configuration files.

How to connect thinkphp to the database

ThinkPHP has a built-in abstract database access layer that encapsulates different database operations. We only need to use the public Db class to operate without having to write different databases for different databases. The code and underlying implementation, the Db class will automatically call the corresponding database driver to process. Current databases include Mysql, SqlServer, PgSQL, Sqlite, Oracle, Ibase, Mongo, and support for PDO.

We have taken the mysql database as an example:

'DB_TYPE'   => 'mysql',         // 数据库类型我们是mysql,就对于的是mysql
'DB_HOST'   => '127.0.0.1',   // 服务器地址,就是我们配置好的php服务器地址,也可以使用localhost,
'DB_NAME'   => 'text',  // 数据库名:mysq创建的要连接我们项目的数据库名称
'DB_USER'   => 'root',           // 用户名:mysql数据库的名称
'DB_PWD'    => '',                 //mysql数据库的 密码
'DB_PORT'   => 3306,            // 端口服务端口一般选3306
'DB_PREFIX' => 'tp_',            //  数据库表前缀
'DB_CHARSET'=> 'utf8',         // 字符集
'DB_DEBUG'  => TRUE,         // 数据库调试模式 开启后可以记录SQL日志 3.2.3新增
Copy after login

How to connect thinkphp to the database

The database has been successfully connected. Try to see if you can access the database correctly. Open the project Home->Controller ->IndexController.class.php

public function index(){
  $user=M('User');   //大M方法访问数据表
  $sql=$user->select();  //thinkPHP 封装的SQL查询所有数据
var_dump($sql);     //打印出数据
}
Copy after login

How to connect thinkphp to the database

Enter http://1270.0.1/text01 in the browser address bar and run the ThinkPHP project to see if the data is printed out. The test is successful. .

How to connect thinkphp to the database

The above is the detailed content of How to connect thinkphp to the database. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template