ecshop数据库操作类

echop没有使用开源的数据库操作类,而是封装了自己的实现,这样做的好处是非常轻量,当网店需要做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为例,介绍数据库操作方法。
在网店根目录下新建文件 test.mysql.class.php:

<?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’) — 查询单个值
 */

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" => "773389925@qq.com", "user_name" => "demo", "password" => "0192025a7bbd73250516f069df18b500", "reg_time" => 1355653990);  
    $db->autoExecute($table, $field_values, "INSERT");   
}

function test_query($table)
{
    global $db;

    $sql = "UPDATE " . $table . "SET user_name='neowuruhua' 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 = 'mantou'";  
    $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 = 'diskson'";  
    $result = $db->getOne($sql);  
    print_r($result);
    echo "<br/>";	
}
?>

输出结果:

Array ( [0] => Array ( [user_name] => admin [email] => 773389925@qq.com ) )
Array ( [user_name] => admin [email] => 773389925@qq.com )
Array ( [0] => 773389925@qq.com )
773389925@qq.com
阅读剩余
THE END