1. Go to this page and download the library: Download colaphp/db library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
//查找一条记录
$db->selectOne("select * from member where mobile = ?",['19812345678']);
$db->selectOne("select * from member where mobile = :mobile",[':mobile'=>'19812345678']);
//查找多条记录
$db->select("select * from member where mobile = ?",['19812345678']);
//插入
$db->insert(
"insert into member (`username`,`mobile`,`created_at`,`updated_at`) values (?, ?, NOW(), NOW());",
['wang3','19812345678']
);
//修改
$db->update(
"update member set `created_at` = :created_at where id = :id",
[ ':created_at' => date('Y-m-d H:i:s'), ':id'=>12 ]
);
//删除
$db->delete("delete from member where id = ?", [16]);
//exec操作
$db->unprepared("delete from member where id = 17");
//常规事务操作
try{
$db->beginTransaction();
//事务数
var_dump($db->transactionLevel());
$db->update(
"update member set `created_at` = :created_at where id = :id",
[ ':created_at' => date('Y-m-d H:i:s'), ':id'=>13 ]
);
$db->insert(
"insert into member (`username`,`mobile`,`created_at`,`updated_at`) values (?, ?, NOW(), NOW());",
['wang','19812345678']
);
$db->commit();
}catch(Exception $e){
$db->rollBack();
var_dump( $e->getMessage() );
}
// Closure事务
$result = $db->transaction(function($me){
$r1 = $me->update(
"update member set `created_at` = :created_at where id = :id",
[ ':created_at' => date('Y-m-d H:i:s'), ':id'=>13 ]
);
$r2 = $me->insert(
"insert into member (`username`,`mobile`,`created_at`,`updated_at`) values (?, ?, NOW(), NOW());",
['wang4','19812345678']
);
return $r1 .'~'. $r2;
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.