摘要:<?php /** * 利用Medoo框架封装简单CURD操作 */ // 1. 实例化Medoo框架类 require __DIR__ . '/connect.php'; // 2. 执行查询 // 表名 $table = 'u
<?php
/**
* 利用Medoo框架封装简单CURD操作
*/
// 1. 实例化Medoo框架类
require __DIR__ . '/connect.php';
// 2. 执行查询
// 表名
$table = 'user';
// 数据
$data['name'] = '东方不败';
$data['sex'] = 1;
$data['age'] = 28;
$data['email'] = 'dfbb@123.com';
$data['password'] = sha1('123456');
$data['create_time'] = time();
$data['status'] = 1;
$fields = ['id', 'name', 'age', 'email'];
// 条件
$where['id'] = 2;
// 添加
function ins($table, $data)
{
global $db;
$stmt = $db->insert($table, $data);
// 查询受影响的记录数量
$num = $stmt->rowCount();
if ($num > 0) {
echo '成功添加了' . $num . '条记录';
} else {
echo '添加失败!!';
}
}
// 更新
function upd($table, $data, $where)
{
global $db;
$stmt = $db->update($table, $data, $where);
// 查询受影响的记录数量
$num = $stmt->rowCount();
if ($num > 0) {
echo '成功修改了' . $num . '条记录';
} else {
echo '修改失败!!';
}
}
// 删除
function del($table, $where)
{
global $db;
$stmt = $db->delete($table, $where);
$num = $stmt->rowCount();
if ($num > 0) {
echo '成功删除了' . $num . '条记录';
} else {
echo '删除失败!!';
}
}
// 查询
function sel($table, $fields, $where)
{
global $db;
$rows = $db->select($table, $fields, $where);
$num = count($rows);
if ($num > 0) {
foreach ($rows as $row) {
echo print_r($row, true) . '<hr>';
}
} else {
echo '删除失败!!';
}
}
批改老师:韦小宝批改时间:2019-02-28 18:02:57
老师总结:写的很不错 课后没事要记得多去练习练习哦