How to link to database through ThinkPHP

jacklove
Release: 2023-04-01 09:40:02
Original
2208 people have browsed it

Make the following configuration in the configuration file to connect to the database

'配置值' 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => 'shop', // 数据库名 'DB_USER' => 'root', // 用户名 'DB_PWD' => '123', // 密码 'DB_PORT' => '3306', // 端口 'DB_PREFIX' => 'sw_', // 数据库表前缀 );
Copy after login

CreateModelModel

Cut the'Home/Model'folder to theApplicationfolder, and letHomeandAdminare used together.

My database indicates that it isgoods. First, create a model class with the same name as the database

GoodsModel.class.php# Methods for instantiating models in ##


        
Copy after login

controller

:

First method:

Define acontroller(GoodsController)To call thisGoodsModel class

'; var_dump($goods); } }
Copy after login

Second type:

UseMfunction for instantiation:

'; var_dump($goods); } }
Copy after login

Third type:

UseDfunction

'; var_dump($goods); } }
Copy after login

M

method is the same asDmethod

M()

Similar tonew Model()

D()

Similar tonew GoodsModel()

Tips: You can see the information of thegoodstable. There is no code written in the model. All business logic is implemented by theModelclass

Operations on tables

Add:M('Table name')-> ;add($date);

Delete:M('Table name')->delete($id);

Update:M('Table name')->save($date);

Query:M('Table Name')->select();

Normal query (display all products)

Code in GoodsController

:

select(); $this->assign('list', $list); $this->display(); } }
Copy after login

Remove from the template

  {$i} {$vo.goods_name} {$vo.goods_number} {$vo.goods_price}   {$vo.goods_brand_id} {$vo.goods_create_time} 修改 删除  
Copy after login
This article explains how to connect to the database through ThinkPHP. For more related content, please pay attention to the php Chinese website.

Related recommendations:

How to connect multiple databases through thinkphp

##About ThinkPHP 5. Some basic operations of the database


Thinkphp5 rules for adding different data

The above is the detailed content of How to link to database through ThinkPHP. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!