ECShop - 数据库操作类
2016-04-07 10:05
387 查看
ECShop v2.7.2没有使用一些开源的数据库操作类,比如adodb或者PEAR,而是封装了自己的实现。这样做的好处是实现非常轻量,只有一个文件,27Kb,大大减小了分发包的文件大小。另外,当网站需要做memcached缓存时,也可以很方便的实现。当然,这样做的后果就是数据库的选择非常狭窄,无法实现其它的非MySQL数据库。
ECShop的数据操作类文件是includes/cls_mysql.php,类名是cls_mysql。该类主要提供了下面 一些比较有用的方法:
getAll($sql)和getAllCached($sql, $cached = 'FILEFIRST'):获取所有记录。
getRow($sql, $limited = false)和getRowCached($sql, $cached = 'FILEFIRST'):获取单行记录。
getCol($sqlse)和getColCached($sql, $cached = 'FILEFIRST'):获取某栏位的所有值。
getOne($sql, $limited = false)和getOneCached($sql, $cached = 'FILEFIRST'):获取单个数值。
query($sql):执行数据库查询。
autoExecute($table, $field_values, $mode = 'INSERT', $where = ''):数据库表操作。
insert_id():获取刚刚插入数据库的新行的id。
现在我们以实例的方式来说明这些方法如何使用。首先,在ecshop/admin目录下新增文件test_mysql.php,文件内容如下:
[php] view plain copy
<?php
define('IN_ECS', true);
define('EC_CHARSET', 'utf-8');
define('ROOT_PATH', 'D:/Program Files/Zend/Apache2/htdocs/ecshop/');
define('DATA_DIR', 'data');
$db_host = "localhost:3306";
$db_name = "ecshop";
$db_user = "root";
$db_pass = "";
require('../includes/cls_mysql.php');
$db = new cls_mysql($db_host, $db_user, $db_pass, $db_name);
将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_getAll();
function test_getAll()
{
global $db;
$sql = "SELECT user_id, user_name, email FROM ecs_admin_user";
$result = $db->getAll($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
Array
(
[0] => Array
(
[user_id] => 1
[user_name] => admin
[email] => admin@admin.com
)
[1] => Array
(
[user_id] => 2
[user_name] => bjgonghuo1
[email] => bj@163.com
)
[2] => Array
(
[user_id] => 3
[user_name] => shhaigonghuo1
[email] => shanghai@163.com
)
[3] => Array
(
[user_id] => 4
[user_name] => amonest
[email] => amonest@foxmail.com
)
)
将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_getRow();
function test_getRow()
{
global $db;
$sql = "SELECT user_id, user_name, email FROM ecs_admin_user LIMIT 1";
$result = $db->getRow($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
Array
(
[user_id] => 1
[user_name] => admin
[email] => admin@admin.com
)
将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_getCol();
function test_getCol()
{
global $db;
$sql = "SELECT email FROM ecs_admin_user";
$result = $db->getCol($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
Array
(
[0] => admin@admin.com
[1] => bj@163.com
[2] => shanghai@163.com
[3] => amonest@foxmail.com
)
将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_getOne();
function test_getOne()
{
global $db;
$sql = "SELECT email FROM ecs_admin_user WHERE user_id = 4";
$result = $db->getOne($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
amonest@foxmail.com
将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_query();
function test_query()
{
global $db;
$sql = "UPDATE ecs_admin_user SET todolist = '你有一封新邮件!' WHERE user_id = 4";
$db->query($sql);
$sql = "SELECT todolist FROM ecs_admin_user WHERE user_id = 4";
$result = $db->getOne($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
你有一封新邮件!
将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_autoExecute();
function test_autoExecute()
{
global $db;
$table = "ecs_role";
$field_values = array("role_name" => "总经理办", "role_describe" => "总经理办", "action_list" => "all");
$db->autoExecute($table, $field_values, "INSERT");
// 执行的SQL:INSERT INTO ecs_role (role_name, action_list, role_describe) VALUES ('总经理办', 'all', '总经理办')
$role_id = $db->insert_id(); // 新记录的ID:5
$field_values = array("action_list" => "goods_manage");
$db->autoExecute($table, $field_values, "UPDATE", "role_id = $role_id");
// 执行的SQL:UPDATE ecs_role SET action_list = 'goods_manage' WHERE role_id = 5
$sql = "SELECT action_list FROM ecs_role WHERE role_id = $role_id";
$result = $db->getOne($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
goods_manage
ECShop的数据操作类文件是includes/cls_mysql.php,类名是cls_mysql。该类主要提供了下面 一些比较有用的方法:
getAll($sql)和getAllCached($sql, $cached = 'FILEFIRST'):获取所有记录。
getRow($sql, $limited = false)和getRowCached($sql, $cached = 'FILEFIRST'):获取单行记录。
getCol($sqlse)和getColCached($sql, $cached = 'FILEFIRST'):获取某栏位的所有值。
getOne($sql, $limited = false)和getOneCached($sql, $cached = 'FILEFIRST'):获取单个数值。
query($sql):执行数据库查询。
autoExecute($table, $field_values, $mode = 'INSERT', $where = ''):数据库表操作。
insert_id():获取刚刚插入数据库的新行的id。
现在我们以实例的方式来说明这些方法如何使用。首先,在ecshop/admin目录下新增文件test_mysql.php,文件内容如下:
[php] view plain copy
<?php
define('IN_ECS', true);
define('EC_CHARSET', 'utf-8');
define('ROOT_PATH', 'D:/Program Files/Zend/Apache2/htdocs/ecshop/');
define('DATA_DIR', 'data');
$db_host = "localhost:3306";
$db_name = "ecshop";
$db_user = "root";
$db_pass = "";
require('../includes/cls_mysql.php');
$db = new cls_mysql($db_host, $db_user, $db_pass, $db_name);
获取所有记录
getAll方法用来从数据库中获取满足条件的所有记录。getAllCached是它的缓存版本,cache key是该方法的第二个参数,如果缓存有效,直接返回缓存结果,否则重新执行数据库查询。将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_getAll();
function test_getAll()
{
global $db;
$sql = "SELECT user_id, user_name, email FROM ecs_admin_user";
$result = $db->getAll($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
Array
(
[0] => Array
(
[user_id] => 1
[user_name] => admin
[email] => admin@admin.com
)
[1] => Array
(
[user_id] => 2
[user_name] => bjgonghuo1
[email] => bj@163.com
)
[2] => Array
(
[user_id] => 3
[user_name] => shhaigonghuo1
[email] => shanghai@163.com
)
[3] => Array
(
[user_id] => 4
[user_name] => amonest
[email] => amonest@foxmail.com
)
)
获取单行记录
getRow方法用来从数据库中获取满足条件的单行记录,或者说是第一条记录。getRowCached是它的缓存版本,cache key是该方法的第二个参数,如果缓存有效,直接返回缓存结果,否则重新执行数据库查询。将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_getRow();
function test_getRow()
{
global $db;
$sql = "SELECT user_id, user_name, email FROM ecs_admin_user LIMIT 1";
$result = $db->getRow($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
Array
(
[user_id] => 1
[user_name] => admin
[email] => admin@admin.com
)
获取某栏位的所有值
getCol方法用来从数据库中获取满足条件的某个栏位的所有值。getColCached是它的缓存版本,cache key是该方法的第二个参数,如果缓存有效,直接返回缓存结果,否则重新执行数据库查询。将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_getCol();
function test_getCol()
{
global $db;
$sql = "SELECT email FROM ecs_admin_user";
$result = $db->getCol($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
Array
(
[0] => admin@admin.com
[1] => bj@163.com
[2] => shanghai@163.com
[3] => amonest@foxmail.com
)
获取单个值
getOne方法用来从数据库中获取满足条件的单个值。getOneCached是它的缓存版本,cache key是该方法的第二个参数,如果缓存有效,直接返回缓存结果,否则重新执行数据库查询。将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_getOne();
function test_getOne()
{
global $db;
$sql = "SELECT email FROM ecs_admin_user WHERE user_id = 4";
$result = $db->getOne($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
amonest@foxmail.com
执行数据库查询
query方法用来执行数据库查询,例如INSERT,UPDATE,DELETE等。将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_query();
function test_query()
{
global $db;
$sql = "UPDATE ecs_admin_user SET todolist = '你有一封新邮件!' WHERE user_id = 4";
$db->query($sql);
$sql = "SELECT todolist FROM ecs_admin_user WHERE user_id = 4";
$result = $db->getOne($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
你有一封新邮件!
数据库表操作
autoExecute方法用来简化对数据表的INSERT和UPDATE。将下面的代码加到test_mysql.php的最后:
[php] view plain copy
test_autoExecute();
function test_autoExecute()
{
global $db;
$table = "ecs_role";
$field_values = array("role_name" => "总经理办", "role_describe" => "总经理办", "action_list" => "all");
$db->autoExecute($table, $field_values, "INSERT");
// 执行的SQL:INSERT INTO ecs_role (role_name, action_list, role_describe) VALUES ('总经理办', 'all', '总经理办')
$role_id = $db->insert_id(); // 新记录的ID:5
$field_values = array("action_list" => "goods_manage");
$db->autoExecute($table, $field_values, "UPDATE", "role_id = $role_id");
// 执行的SQL:UPDATE ecs_role SET action_list = 'goods_manage' WHERE role_id = 5
$sql = "SELECT action_list FROM ecs_role WHERE role_id = $role_id";
$result = $db->getOne($sql);
print_r($result);
}
修改以后的test_mysql.php执行结果如下:
[php] view plain copy
goods_manage
相关文章推荐
- 详解SQL Server连接(内连接、外连接、交叉连接)
- 在Windows7下启动MongoDB服务的解决方案
- SQL实现Split功能
- django 直接执行SQL
- NopCommerce架构分析之(三)EntityFramework数据库初试化及数据操作
- Linux下编译redis和phpredis的方法
- 数据库优化之表结构优化
- Oracle计算时间差函数
- win7启动本地MongoDB的四种方式
- Oracle密码过期the password has expired解决办法
- VS2008 IDE连接 ORACLE后新建查询 无响应或者长时间等待
- MySQL函数介绍
- mysql对数据库多个字段同时进行模糊查询
- MongoDB的简单操作(asp.net)
- pl/sql过期问题
- mysql TIMESTAMP 不能为NULL
- mybatis--动态sql中的where语句
- C#连接mysql数据库插入数据后获取自增长主键ID值
- plsql无法中文条件查询
- 10、Mysql的主从复制