Home > Article > Backend Development > How to link to database through ThinkPHP
Make the following configuration in the configuration file to connect to the database
<?php return array( //'配置项'=>'配置值' 'DB_TYPE' => 'mysql', // 数据库类型 'DB_HOST' => 'localhost', // 服务器地址 'DB_NAME' => 'shop', // 数据库名 'DB_USER' => 'root', // 用户名 'DB_PWD' => '123', // 密码 'DB_PORT' => '3306', // 端口 'DB_PREFIX' => 'sw_', // 数据库表前缀 );
Cut the 'Home/Model' folder to the Application folder, and let Home and Admin are used together.
My database indicates that it is goods. First, create a model class with the same name as the database
GoodsModel.class.php# Methods for instantiating models in ##
<?php namespace Model; use Think\Model; class GoodsModel extends Model{ }controller
:
First method:
Define a controller(GoodsController)To call thisGoodsModel class
<?php namespace Admin\Controller; use Model\GoodsModel; use Think\Controller; class GoodsController extends Controller{ public function test1(){ $goods = new GoodsModel(); echo '<pre class="brush:php;toolbar:false">'; var_dump($goods); } }
Second type:
Use M function for instantiation:
<?php namespace Admin\Controller; use Model\GoodsModel; use Think\Controller; class GoodsController extends Controller{ public function test1(){ $goods = M('goods'); echo '<pre class="brush:php;toolbar:false">'; var_dump($goods); } }
Third type:
Use Dfunction
<?php namespace Admin\Controller; use Model\GoodsModel; use Think\Controller; class GoodsController extends Controller{ public function test1(){ $goods = D('goods'); echo '<pre class="brush:php;toolbar:false">'; var_dump($goods); } }M
method is the same as Dmethod
M()Similar to new Model()
D()Similar tonew GoodsModel()
Tips: You can see the information of the goods table. There is no code written in the model. All business logic is implemented by the Model class
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:
<?php namespace Admin\Controller; use Model\GoodsModel; use Think\Controller; class GoodsController extends Controller{ public function showlist(){ $list = M('goods')->select(); $this->assign('list', $list); $this->display(); } }
Remove from the template
<volist name="list" id="vo" > <tr id="product1"> <td>{$i}</td> <td><a href="#">{$vo.goods_name}</a></td> <td>{$vo.goods_number}</td> <td>{$vo.goods_price}</td> <td><img src="../../../Application/Admin/Public/img/20121018-174034-58977.jpg" height="60" width="60"></td> <td><img src="../../../Application/Admin/Public/img/20121018-174034-97960.jpg" height="40" width="40"></td> <td>{$vo.goods_brand_id}</td> <td>{$vo.goods_create_time}</td> <td><a href="#">修改</a></td> <td><a href="javascript:;" onclick="delete_product(1)">删除</a></td> </tr> </volist>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
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!