Thinkphp5 Framework Realizes the Method of Obtaining Database Data to View

  • 2021-12-13 16:33:40
  • OfStack

In this paper, an example is given to describe the method of obtaining database data to view by Thinkphp5 framework. Share it for your reference, as follows:

This is the basic note for learning thinkhp5.

This is about configuring database links, querying database data, and finally assigning data to views.

Database configuration:

The database configuration for thinkphp5 defaults to database. php under conf. My database configuration items are as follows


<?php
 return [
    //  Database type 
    'type'      => 'mysql',
    //  Database connection DSN Configure 
    'dsn'       => '',
    //  Server address 
    'hostname'    => '127.0.0.1',
    //  Database name 
    'database'    => 'course',
    //  Database user name 
    'username'    => 'root',
    //  Database password 
    'password'    => '',
    //  Database connection port 
    'hostport'    => '3306',
    //  Database connection parameters 
    'params'     => [],
    //  Database encoding is adopted by default utf8
    'charset'     => 'utf8',
    //  Database table prefix 
    'prefix'     => 'imooc_',
    //  Database debugging mode 
    'debug'      => false,
    //  Database deployment mode :0  Centralized type ( Single 1 Server ),1  Distributed ( Master-slave server )
    'deploy'     => 0,
    //  Is the database read and write separated   Master-slave validity 
    'rw_separate'   => false,
    //  After separation of reading and writing   Number of primary servers 
    'master_num'   => 1,
    //  Specify slave server sequence number 
    'slave_no'    => '',
    //  Do you strictly check the existence of fields 
    'fields_strict'  => true,
    //  Dataset return type 
    'resultset_type' => 'array',
    //  Automatically write the timestamp field 
    'auto_timestamp' => false,
    //  The default time format after the time field is taken out 
    'datetime_format' => 'Y-m-d H:i:s',
    //  Does it need to be carried out SQL Performance analysis 
    'sql_explain'   => false,
    // Builder Class 
    'builder'     => '',
    // Query Class 
    'query'      => '\\think\\db\\Query',
];

The main configuration items are database server address hostname, database name database, database user name username, database password password, and a table prefix prefix. After configuration, you can query the database using the query statement of tp5.

Query database data phase, the use of tp5 model class, so you can directly use tp5 with the database query method, the following is model code


<?php
namespace app\index\model;
use think\Model;
class Course extends Model{
}

Then the model code is introduced into the method of the controller, and it can be used. The code used is as follows, and it should be introduced before use


<?php
namespace app\index\controller;
use think\Controller;
use app\index\model\Course;
//use think\Loader;
class Index extends Controller
{
  public function index()
  {
     $res=Course::get(2)->toArray();
     /*return view("index",[
       "email"=>"123456@163.com"
     ]);*/
     return view("index",$res);
}

That's it, haha; Many places that have not been explained clearly seem to. There are two ways for tp5 to operate database, and there are three methods in detail. The two methods are to use Db class and inherit database model; Three are queries that can use tp under Db or native queries.

Note that find and select are methods for the query constructor, and get and all are methods for the model. However, the model is based on the query constructor, so the model can call find and select methods, but the query constructor cannot call get and all methods.

If the Db class is used, the above implementation becomes


<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
//use think\Loader;
class Index extends Controller
{
  public function index()
  {
     $res=Db::table("imooc_course")
     ->find(2);
     /*return view("index",[
       "email"=>"123456@163.com"
     ]);*/
     return view("index",$res);
}

Note that the find method returns an array form directly, and no array conversion is required.

There are many methods about tp5 to manipulate the database, and here we just record the basic operation realization of querying the database.

For more readers interested in thinkPHP related contents, please check the topics of this site: "Introduction to ThinkPHP", "Summary of thinkPHP Template Operation Skills", "Summary of ThinkPHP Common Methods", "Introduction to codeigniter", "Advanced Course of CI (CodeIgniter) Framework", "Introduction to Zend FrameWork Framework" and "Summary of PHP Template Technology".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: