
1、在Db.class.php脚本文件里面的类增加一个魔术方法__get(),写法如下:
public function __get($propertyName)
{ return $this->$propertyName
}
这个方法是用来访问类中protected $config成员属性用的。有的人可能会说,直接把protected改成public岂不是更好。这样只解决了基类的问题,假如,子类也同样进行了受保护,那要你更改更多的文件,这是我们做IT程序员非常不愿意看到的事情。
2、在Model.class.php中的getTableName()方法更改如下:
$tablepre = $this->db->config['tablepre']
if(empty($this->trueTableName)) {
$tableName??= empty($tablepre) ? $this->tablePrefix : $tablepre
if(!empty($this->tableName)) {
$tableName .= $this->tableName
}
else
{
$tableName .= parse_name($this->name)
}
$this->trueTableName? ? =? ?strtolower($tableName)
}
return (!empty($this->dbName)?$this->dbName.'.':'').$this->
trueTableName这样就完成了多库自由切换时,导致的表前缀问题。
/*******************面向对象PDO连接方式*********************/
'DB_TYPE' =>'PDO', // 数据库类型
'DB_DSN' =>'mysql:host=localhostdbname=master', // DSN连接。
'DB_USER' =>'root', // 数据库用户名
'DB_PWD' =>'123456', // 数据库密码
'DB_PORT' =>'3306', // 数据库端口
'DB_PREFIX' =>'g_', // 数据表前缀
'DB_CHARSET' =>'utf8', // 数据库编码默认采用utf8
thinkphp3.x连接mysql数据库的方法。分享给大家供大家参考,具体如下:惯例配置文件:ThinkPHP/conf/convention.php
(1)在配置文件中填写配置信息(配置文件:“./xmall/conf/config.php”):
示例:
<?php return array(//'配置项'=>'配置值'/* 数据库设置 */'DB_TYPE'=>'mysql', // 数据库类型 'DB_HOST'=>'localhost', // 服务器地址 'DB_NAME'=>'xmall', // 数据库名 'DB_USER'=>'root', // 用户名 'DB_PWD'=>'123', // 密码 'DB_PORT'=>'3306', // 端口 'DB_PREFIX'=>'think_', // 数据库表前缀 'DB_FIELDTYPE_CHECK' =>false, // 是否进行字段类型检查 'DB_FIELDS_CACHE' =>true, // 启用字段缓存 'DB_CHARSET' =>'utf8', // 数据库编码默认采用utf8 )?>
(2)创建表:
?12345 CREATE TABLE `think_user` ( `id` int(11) DEFAULT NULL, `name` varchar(30) DEFAULT NULL, `pwd` varchar(20) DEFAULT NULL) ENGINE=InnoDB
(3) 执行数据插入 *** 作在lib/Action下修改IndexAction.class.php文件,内容如下:
<?php class IndexAction extends Action{function index(){ public function index(){$data=array( "id"=>"1", "name="=>"liuning", "pwd"=>"asd123" ) M("user")->add($data) }} } ?>
(4)执行http://localhost/xmall/index.php,数据库中就会有新的记录生成;
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)