zoukankan      html  css  js  c++  java
  • ECShop

    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
    1. <?php  
    2.   
    3. define('IN_ECS', true);   
    4. define('EC_CHARSET', 'utf-8');  
    5. define('ROOT_PATH', 'D:/Program Files/Zend/Apache2/htdocs/ecshop/');  
    6. define('DATA_DIR', 'data');  
    7.   
    8. $db_host = "localhost:3306";   
    9. $db_name = "ecshop";   
    10. $db_user = "root";   
    11. $db_pass = "";   
    12.   
    13. require('../includes/cls_mysql.php');   
    14. $db = new cls_mysql($db_host, $db_user, $db_pass, $db_name);  

     获取所有记录

    getAll方法用来从数据库中获取满足条件的所有记录。getAllCached是它的缓存版本,cache key是该方法的第二个参数,如果缓存有效,直接返回缓存结果,否则重新执行数据库查询。

    将下面的代码加到test_mysql.php的最后:

    [php] view plain copy
    1. test_getAll();  
    2.   
    3. function test_getAll()  
    4. {  
    5.     global $db;  
    6.       
    7.     $sql = "SELECT user_id, user_name, email FROM ecs_admin_user";  
    8.     $result = $db->getAll($sql);  
    9.     print_r($result);  
    10. }  

    修改以后的test_mysql.php执行结果如下:

    [php] view plain copy
    1. Array  
    2. (  
    3.     [0] => Array  
    4.         (  
    5.             [user_id] => 1  
    6.             [user_name] => admin  
    7.             [email] => admin@admin.com  
    8.         )  
    9.   
    10.     [1] => Array  
    11.         (  
    12.             [user_id] => 2  
    13.             [user_name] => bjgonghuo1  
    14.             [email] => bj@163.com  
    15.         )  
    16.   
    17.     [2] => Array  
    18.         (  
    19.             [user_id] => 3  
    20.             [user_name] => shhaigonghuo1  
    21.             [email] => shanghai@163.com  
    22.         )  
    23.   
    24.     [3] => Array  
    25.         (  
    26.             [user_id] => 4  
    27.             [user_name] => amonest  
    28.             [email] => amonest@foxmail.com  
    29.         )  
    30.   
    31. )  

     获取单行记录

    getRow方法用来从数据库中获取满足条件的单行记录,或者说是第一条记录。getRowCached是它的缓存版本,cache key是该方法的第二个参数,如果缓存有效,直接返回缓存结果,否则重新执行数据库查询。

    将下面的代码加到test_mysql.php的最后:

    [php] view plain copy
    1. test_getRow();  
    2.   
    3. function test_getRow()  
    4. {  
    5.     global $db;  
    6.       
    7.     $sql = "SELECT user_id, user_name, email FROM ecs_admin_user LIMIT 1";  
    8.     $result = $db->getRow($sql);  
    9.     print_r($result);  
    10. }  

    修改以后的test_mysql.php执行结果如下:

    [php] view plain copy
    1. Array  
    2. (  
    3.     [user_id] => 1  
    4.     [user_name] => admin  
    5.     [email] => admin@admin.com  
    6. )  

     获取某栏位的所有值

    getCol方法用来从数据库中获取满足条件的某个栏位的所有值。getColCached是它的缓存版本,cache key是该方法的第二个参数,如果缓存有效,直接返回缓存结果,否则重新执行数据库查询。

    将下面的代码加到test_mysql.php的最后:

    [php] view plain copy
    1. test_getCol();  
    2.   
    3. function test_getCol()  
    4. {  
    5.     global $db;  
    6.       
    7.     $sql = "SELECT email FROM ecs_admin_user";  
    8.     $result = $db->getCol($sql);  
    9.     print_r($result);  
    10. }  

    修改以后的test_mysql.php执行结果如下:

    [php] view plain copy
    1. Array  
    2. (  
    3.     [0] => admin@admin.com  
    4.     [1] => bj@163.com  
    5.     [2] => shanghai@163.com  
    6.     [3] => amonest@foxmail.com  
    7. )  

     获取单个值

    getOne方法用来从数据库中获取满足条件的单个值。getOneCached是它的缓存版本,cache key是该方法的第二个参数,如果缓存有效,直接返回缓存结果,否则重新执行数据库查询。

    将下面的代码加到test_mysql.php的最后:

    [php] view plain copy
    1. test_getOne();  
    2.   
    3. function test_getOne()  
    4. {  
    5.     global $db;  
    6.       
    7.     $sql = "SELECT email FROM ecs_admin_user WHERE user_id = 4";  
    8.     $result = $db->getOne($sql);  
    9.     print_r($result);  
    10. }  

    修改以后的test_mysql.php执行结果如下:

    [php] view plain copy
    1. amonest@foxmail.com  

     执行数据库查询

    query方法用来执行数据库查询,例如INSERT,UPDATE,DELETE等。 

    将下面的代码加到test_mysql.php的最后:

    [php] view plain copy
    1. test_query();  
    2.   
    3. function test_query()  
    4. {  
    5.     global $db;  
    6.       
    7.     $sql = "UPDATE ecs_admin_user SET todolist = '你有一封新邮件!' WHERE user_id = 4";  
    8.     $db->query($sql);  
    9.     $sql = "SELECT todolist FROM ecs_admin_user WHERE user_id = 4";  
    10.     $result = $db->getOne($sql);  
    11.     print_r($result);  
    12. }  

    修改以后的test_mysql.php执行结果如下:

    [php] view plain copy
    1. 你有一封新邮件!  

     数据库表操作

    autoExecute方法用来简化对数据表的INSERT和UPDATE。 

    将下面的代码加到test_mysql.php的最后:

    [php] view plain copy
    1. test_autoExecute();  
    2.   
    3. function test_autoExecute()  
    4. {  
    5.     global $db;  
    6.       
    7.     $table = "ecs_role";  
    8.     $field_values = array("role_name" => "总经理办", "role_describe" => "总经理办", "action_list" => "all");  
    9.     $db->autoExecute($table, $field_values, "INSERT");  
    10.     // 执行的SQL:INSERT INTO ecs_role (role_name, action_list, role_describe) VALUES ('总经理办', 'all', '总经理办')  
    11.   
    12.     $role_id = $db->insert_id(); // 新记录的ID:5  
    13.       
    14.     $field_values = array("action_list" => "goods_manage");  
    15.     $db->autoExecute($table, $field_values, "UPDATE", "role_id = $role_id");  
    16.     // 执行的SQL:UPDATE ecs_role SET action_list = 'goods_manage' WHERE role_id = 5  
    17.   
    18.     $sql = "SELECT action_list FROM ecs_role WHERE role_id = $role_id";  
    19.     $result = $db->getOne($sql);  
    20.     print_r($result);  
    21. }  

    修改以后的test_mysql.php执行结果如下:

    [php] view plain copy
    1. goods_manage  
  • 相关阅读:
    ZOJ 1002 Fire Net
    Uva 12889 One-Two-Three
    URAL 1881 Long problem statement
    URAL 1880 Psych Up's Eigenvalues
    URAL 1877 Bicycle Codes
    URAL 1876 Centipede's Morning
    URAL 1873. GOV Chronicles
    Uva 839 Not so Mobile
    Uva 679 Dropping Balls
    An ac a day,keep wa away
  • 原文地址:https://www.cnblogs.com/dhsx/p/5362340.html
Copyright © 2011-2022 走看看