前篇文章中,我提及到了如何《提取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;