ECSHOP没有使用开源的数据库操作类,而是封装了自己的实现,这样做的好处是非常轻量,当网店需要做memcached缓存时,也可以方便的实现。当然,这样做的坏处是对数据库的选择非常狭窄,无法实现对非MySQL数据库的操作。
数据库操作类文件是includes/cls_mysql.php,该类提供了一些比较简便的方法。
autoExecute($table, $field_values, $mode = ‘INSERT’, $where = ”) — 数据库表操作
query($sql) — 数据库查询
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’) — 查询单个值
本文以ECSHOP会员表users为例,介绍数据库操作方法。
操作之前,数据库只有一条记录,其中user_name=”test”,email=”test#gmail.com”。
在网店根目录下新建文件test.mysql.class.php:
<?php define('IN_ECS', true); require(dirname(__FILE__) . '/includes/init.php'); $table = $ecs->table("users"); test_autoExecute($table); //数据表操作 test_query($table); //数据库查询 test_getAll($table); //查询所有记录 test_getRow($table); //查询单行记录 test_getCol($table); //查询某栏位的所有值 test_getOne($table); //查询单个值 function test_autoExecute($table) { global $db; $field_values = array("email" => "newsletter#domain.com", "user_name" => "demo", "password" => "0192023a7bbd73250516f069df18b500", "reg_time" => 1355693990); $db->autoExecute($table, $field_values, "INSERT"); } function test_query($table) { global $db; $sql = "UPDATE " . $table . "SET user_name='phpally' WHERE user_name = 'demo'"; $db->query($sql); } function test_getAll($table) { global $db; $sql = "SELECT user_name, email FROM " . $table; $result = $db->getAll($sql); print_r($result); echo "<br/>"; } function test_getRow($table) { global $db; $sql = "SELECT user_name, email FROM " . $table . " WHERE user_name = 'test'"; $result = $db->getRow($sql); print_r($result); echo "<br/>"; } function test_getCol($table) { global $db; $sql = "SELECT email FROM " . $table; $result = $db->getCol($sql); print_r($result); echo "<br/>"; } function test_getOne($table) { global $db; $sql = "SELECT email FROM " . $table . " WHERE user_name = 'test'"; $result = $db->getOne($sql); print_r($result); echo "<br/>"; } ?>
输出:
Array ( [0] => Array ( [user_name] => phpally [email] => newsletter#domain.com ) [1] => Array ( [user_name] => test [email] => test#gmail.com ) ) Array ( [user_name] => test [email] => test#gmail.com ) Array ( [0] => test#gmail.com [1] => newsletter#domain.com ) test#gmail.com
转载~