連接數(shù)據(jù)庫
修改(C:\AppServ\www\tp5\application\database.php)
// 數(shù)據(jù)庫類型
'type' => 'mysql',
// 服務(wù)器地址
'hostname' => '127.0.0.1',
// 數(shù)據(jù)庫名
'database' => 'yzmedu',
// 用戶名
'username' => 'root',
// 密碼
'password' => '8096233.',
// 端口
'hostport' => '3306',
前臺(tái)控制器中
use think\Db;
public function data(){
//實(shí)例化系統(tǒng)數(shù)據(jù)庫
$DB= new Db;
//方法一 查詢數(shù)據(jù) 系統(tǒng)方法
$data=$DB::table("user")->select();
//方法二 使用sql語句
$data=$DB::query("select*from user");
dump($data);
}
操作
//聲明命名空間
namespace app\index\controller;
//導(dǎo)入系統(tǒng)控制器類
use think\Controller;
//引入系統(tǒng)類
use think\Db;
//聲明控制器類
class User extends Controller{
......
.....
}
瀏覽器訪問http://localhost/tp5/public/index.php/index/User/select
1、增
public function insert(){
//執(zhí)行查詢語句
//返回值為影響行數(shù)
$data=Db::execute("insert into user value(null,'user','123','66')");
$data=Db::execute("insert into user value(null,?,?,?)",['user2','456','20']);
$data=Db::execute("insert into user value(null,:name,:pass,:age)",['name'=>"user3",'pass'=>"678",'age'=>"224"]);
dump($data);
}
2、刪
public function delete(){
//返回值影響行數(shù)
//$data=Db::execute('delete from user where id=10');
// $data=Db::execute("delete from user where id>?",[15]);
$data=Db::execute("delete from user where id>:id",["id"=>5]);
dump($data);
}
3、改
//改
public function update(){
$data=Db::execute("update user set age='20' where id=9");
dump($data);
}
4、查
public function select(){
//查詢數(shù)據(jù)
$data=DB::query("select*from user");
//dump($data);
$data=Db::query("select*from user where id>=? and id<=?",[5,8]);
dump($data);
}
5、獲取執(zhí)行的sql語句
echo Db::getLastSql();