tp5 of thinkPHP5 Framework Connect to Database Method Example
- 2021-11-10 09:11:55
- OfStack
This paper describes the method of connecting database with thinkPHP5 framework. Share it for your reference, as follows:
1. Configuration file directory tp5\ application\ database. php
Connect through a configuration file. .
You can also link through methods
The method links the database in the controller; There is a slight difference between the writing method of query and the DB method of using the system
// Configuring a database connection using methods
public function data1 ()
{
$DB = Db::connect([
// Database type
'type' => 'mysql',
// Server address
'hostname' => '127.0.0.1',
// Database name
'database' => 'user',
// User name
'username' => 'root',
// Password
'password' => 'root',
// Port
'hostport' => '3306',
]);
// dump($DB);
// Query data,,,,, and use the system's DB Class methods differ slightly
$data = $DB -> table("uu") -> select();
dump($data);
}
2. Basic use, addition, deletion and revision
The controller uses the configuration file to connect to the database
The file under the controller (tp5\ application\ index\ controller\ Index.php) is written to
<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
class Index extends Controller
{
public function index()
{
// return ' Come to class ';
return $this -> fetch();
}
// Connecting to a database using a configuration file
public function data()
{
// Instantiate database system class
$DB = new Db;
// Query data, the table name is uu All data of
$data = $DB::table("uu") -> select();
// Use sql Statement
//$data = $DB::query("select * from uu");
dump($data);
}
}
http://yourwebname/public/index. php/index/Index/data Acquisition Data Print Test
3. Render the data to the template page
<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
// Use model Connect to the database to introduce moadel
use think\Model;
class Index extends Controller
{
public function index()
{
// return 's';
$this -> data();
return $this -> fetch();
}
// Connecting to a database using a system configuration file
public function data()
{
// Instantiate database system class
$DB = new Db;
// Query data
$data = $DB::table("uu") -> select();
$this -> assign("user",$data);
// dump($data);
}
}
4. The template page can reference rendering data
tp5\application\index\view\index\index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>s</title>
</head>
<body>
<div> s</div>
{volist name="user" id="vo"}
<a href="">{$vo.name}</a>
{/volist}
</body>
</html>
Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to PHP programming based on ThinkPHP framework.