TA的每日心情 | 奋斗 2024-12-23 00:39 |
---|
签到天数: 3 天 连续签到: 1 天 [LV.2]偶尔看看I 766680204
|
本帖最后由 飞凤互联 于 2024-12-19 00:39 编辑
php 自定义数据库函数支持增删改查4类 方便在自己开发的时候需要操作数据库 - // 假设你已经有了数据库连接 $conn 和表前缀 $db_prefix
-
- /**
- * 通用的数据库操作函数
- *
- * @param string $conn 数据库连接对象
- * @param string $db_prefix 表前缀
- * @param string $action 操作类型:select, insert, update, delete
- * @param string $table 表名(不含前缀)
- * @param array $data 操作涉及的数据(对于select,这是查询条件;对于insert/update,这是要设置的数据)
- * @param array $where 查询或更新条件(仅对select和update有用)
- * @return mixed 查询结果或操作影响的行数,或者false(如果操作失败)
- */
- function dbOperation($conn, $db_prefix, $action, $table, $data = [], $where = []) {
- $table = "`{$db_prefix}{$table}`";
-
- switch ($action) {
- case 'select':
- $sql = "SELECT * FROM {$table}";
- if (!empty($where)) {
- $sql .= " WHERE " . implode(' AND ', array_map(function($key) use ($where) {
- return "`{$key}`=?";
- }, array_keys($where)));
- }
- $stmt = $conn->prepare($sql);
- if (!empty($where)) {
- $types = str_repeat('s', count($where));
- $stmt->bind_param($types, ...array_values($where));
- }
- break;
-
- case 'insert':
- $keys = implode(', ', array_keys($data));
- $values = implode("', '", array_values($data));
- $sql = "INSERT INTO {$table} ({$keys}) VALUES ('{$values}')";
- $stmt = $conn->prepare($sql);
- break;
-
- case 'update':
- $set = implode(', ', array_map(function($key, $value) {
- return "`{$key}`=?";
- }, array_keys($data), array_values($data)));
- $sql = "UPDATE {$table} SET {$set} WHERE " . implode(' AND ', array_map(function($key) use ($where) {
- return "`{$key}`=?";
- }, array_keys($where)));
- $types = str_repeat('s', count($data) + count($where));
- $stmt = $conn->prepare($sql);
- $mergedData = array_merge(array_values($data), array_values($where));
- $stmt->bind_param($types, ...$mergedData);
- break;
-
- case 'delete':
- $sql = "DELETE FROM {$table} WHERE " . implode(' AND ', array_map(function($key) use ($where) {
- return "`{$key}`=?";
- }, array_keys($where)));
- $types = str_repeat('s', count($where));
- $stmt = $conn->prepare($sql);
- $stmt->bind_param($types, ...array_values($where));
- break;
-
- default:
- return false;
- }
-
- // 执行查询或操作
- $result = $stmt->execute();
-
- // 根据操作类型返回结果
- if ($action === 'select') {
- return $stmt->get_result();
- } else {
- return $stmt->affected_rows;
- }
- }
复制代码
使用方法
- // 示例用法
- $table = 'plugins';
- $data = ['plugin_name' => '飞凤互联'];
- $where = ['id' => 1];
-
- // 查询
- $result = dbOperation($conn, $db_prefix, 'select', $table, [], $where);
- if ($result) {
- while ($row = $result->fetch_assoc()) {
- // 处理结果
- }
- }
-
- // 插入
- $data = ['plugin_name' => 'new_plugin', 'status' => 'active'];
- $insertResult = dbOperation($conn, $db_prefix, 'insert', $table, $data);
- if ($insertResult) {
- echo "插入成功,影响了{$insertResult}行";
- }
-
- // 更新
- $data = ['status' => 'inactive'];
- $updateResult = dbOperation($conn, $db_prefix, 'update', $table, $data, $where);
- if ($updateResult) {
- echo "更新成功,影响了{$updateResult}行";
- }
-
- // 删除
- $deleteResult = dbOperation($conn, $db_prefix, 'delete', $table, [], $where);
- if ($deleteResult) {
- echo "删除成功,影响了{$deleteResult}行";
- }
复制代码
|
|