完善ecshop的mysql类

前篇文章中,我提及到了如何《提取ecshop的mysql类》。但是没有数据库前缀的写法

废话不说,上步骤(目录结构请参考提取ecshop的mysql类

修改connfig.php为

 

<?php 
header("Content-Type:text/html;charset=utf-8");
date_default_timezone_set("Etc/GMT-8");
error_reporting(E_ALL ^ E_NOTICE);
global $db;

define('IN_ECS', true);
define('EC_CHARSET', 'utf-8');
define('ROOT_PATH', dirname(__FILE__).'\..\\');
define('DATA_DIR', 'data');


$db_host = "localhost:3306";
$db_name = "duizhang";
$db_user = "root";
$db_pass = "";
$prefix  = "mydb_";

require(ROOT_PATH.'Classes/cls_mysql.php');
$db = new cls_mysql($db_host, $db_user, $db_pass, $db_name, $prefix);

?>

 

 

修改/Classes/cls_mysql.php

搜索var $timezone,在下面添加

 

    var $db_name        = '';
    var $prefix         = '';

 

 

 

搜索function __construct,修改为(注意红色字体

    function __construct($dbhost, $dbuser, $dbpw, $dbname = '', $prefix = '', $charset = 'gbk', $pconnect = 0, $quiet = 0)
    {
        $this->db_name = $dbname;
        $this->prefix = $prefix;
$this->cls_mysql($dbhost, $dbuser, $dbpw, $dbname, $prefix, $charset, $pconnect, $quiet); }

 

搜索function cls_mysql,修改为(注意红色字体)

function cls_mysql($dbhost, $dbuser, $dbpw, $dbname = '', $prefix = '', $charset = 'gbk', $pconnect = 0, $quiet = 0)

 

搜索'dbname'   => $dbname, 在下面添加

'prefix'   => $prefix,

 

搜索function select_database,在上面添加

    function table($str)
    {
        return '`' . $this->db_name . '`.`' . $this->prefix . $str . '`';
    }

 

 

修改完毕。测试即可

$sql = 'SELECT * FROM '.$db->table('member').' WHERE 1';
echo $sql;exit;

 

posted @ 2015-04-30 10:16  神马和浮云  阅读(2582)  评论(0编辑  收藏  举报