日期:2014-05-16 浏览次数:20621 次
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 = ''):数据库表操作。
现在我们以实例的方式来说明这些方法如何使用。首先,在ecshop/admin目录下新增文件test_mysql.php,文件内容如下:view plaincopy to clipboardprint?
<?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);?
<?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的最后:
view plaincopy to clipboardprint?
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_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执行结果如下:
view plaincopy to clipboardprint?
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??
??????? )??
?
)?
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
??????? )
)
?
--------------------------------------------
?获取单行记录