ThinkPHP5.1 Framework Database Link and Addition Deletion Modification and Search Operation Example

  • 2021-12-13 07:47:04
  • OfStack

In this paper, an example is given to describe the operation of linking, adding, deleting, modifying and checking the database of ThinkPHP5.1 framework. Share it for your reference, as follows:

1. How the database is linked


<?php
namespace app\index\controller;
use think\Db;
class Demo
{
//1 Global configuration  config/database.php Configure 
public function dbTest()
{
return Db::table('pzq_article')
->where('id','29')
->value('title');
}
//2 Dynamic configuration  think\db\Query.php Among them 1 Methods connect()
public function dbTest2()
{
return Db::connect([
'type'=>'mysql',
'hostname'=>'localhost',
'database'=>'top789',
'username'=>'root',
'password'=>'root',
])
->table('pzq_article')
->where('id','76')
->value('title');
}
//3 , DSN Connect 
public function dbTest3()
{
$dsn = 'mysql://root:root@localhost:3306/top789#utf8';
return Db::connect($dsn)
->table('pzq_article')
->where('id','88')
->value('title');
}
//4 , single check ten days 
public function dbTest4()
{
$res = Db::table('pzq_article')
->field(['title'=>' Title ','id'=>' Numbering '])// Alias can be added 
->where('id','=',20)// If it's an equal sign, = Can be omitted 
->find();// If it is a primary key query, you can omit the above where, This line says ->find(20);
dump(is_null($res)?' Didn't find out ':$res);
}
//5 , multiple check ten days 
public function dbTest5()
{
$res = Db::table('pzq_article')
->field(['id','cat_id','title'])
->where([
['id','>',20],
['cat_id','=',2],
])//1 Condition, directly using the expression ->where('id','>',20) . Array for multiple conditions 
->order('id desc')->limit(3)->select();
if(empty($res)){
return ' Didn't find out ';
}else{
dump($res);
}
}
//6 Data addition 
public function dbTest6()
{
$data = [
'name'=>'Sam2',
'age'=>'29',
'posttime'=>time()
];
$dataall=[
['name'=>'Sam3','age'=>'29','posttime'=>time()],
['name'=>'Sam4','age'=>'30','posttime'=>time()],
];
//(1) Single insert 
//return Db::table('test')->data($data)->insert();
//(2) Insert and return the new primary key id
//return Db::table('test')->insertGetId($data);
//(3) Insert multiple pieces of data 
return Db::table('test')->data($dataall)->insertAll();
}
// Update data 
public function dbTest7()
{
// return Db::table('test')
// ->where('id','=',4)
// ->update(['name'=>'SamC','age'=>'31']);
// If where The condition is the primary key, and you can also use the following 
return Db::table('test')
->update(['name'=>'SamCheng','age'=>'30','id'=>4]);
}
// Delete operation 
public function dbTest8()
{
//return Db::table('test')->delete(6);
// Or 
return Db::table('test')->where('id',5)->delete();
}
//mysql Native statement   Query 
public function dbTest9()
{
$sql = "select name,age from test where id>2";
dump(Db::query($sql));
}
//mysql  Add, delete and change   Use Db::execute($sql)
public function dbTest10()
{
//$sql = "update test set name='samC' where id=4";
//$sql = "insert test set name='Yan',age='30'";
$sql = "delete from test where id=4";
return Db::execute($sql);
}
}

For more readers interested in thinkPHP related contents, please 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".

Hope that this article is based on the framework of ThinkPHP PHP programming help.


Related articles: