YII的PHP应用程序 -- 深入研究 1 -- 操作数据库
时间:2009-06-17 来源:liu1084
建立一个应用程序,修改config/main.php配置文件:
修改:
'db'=>array(
'connectionString'=>'Your DSN',
),
为:
'db'=>array(
'class' => 'CDbConnection',
'connectionString'=>'mysql:host=localhost;port=3306;dbname=center',
'username' => 'root',
'password' => '×××××××××××',
),
我们通过这个配置,连自动连接到mysql数据库。
打开默认的模型文件:
LoginForm.php,看到登录用户用到了
$identity=new UserIdentity($this->username,$this->password);
修改UserIdentity.php:
添加数据库验证部分:
private function checkUser(){
$connection = Yii::app()->db;
$connection->active = true;
$sql = "SELECT * FROM `member` WHERE `username` = '" . $this->username .
"' AND `md5Password` = '" . md5($this->password) . "'" ;
$command = $connection->createCommand($sql);
$reader=$command->query();
if ($reader->getRowCount() > 0){
return self::ERROR_NONE;
}
}
修改:
public function authenticate()
{
if(!isset($this->username)){
$this->errorCode=self::ERROR_USERNAME_INVALID;
}else{
$this->errorCode = $this->checkUser();
}
return $this->errorCode;
}
OK,现在登录,应该可以成功了吧。
表结构:
CREATE TABLE `member` (
`id` bigint(30) unsigned NOT NULL auto_increment COMMENT '自增',
`pid` int(11) NOT NULL COMMENT '用户对应的属性ID',
`username` varchar(32) NOT NULL COMMENT '用户名,可以是:用户名、email、通行证、手机号、身份证号码',
`usernameType` tinyint(5) NOT NULL COMMENT '对应member_type的ID',
`md5Password` varchar(32) NOT NULL COMMENT '加密密码',
`activeDatetime` datetime default NULL COMMENT '激活时间',
`loginErrorTimes` tinyint(5) default '0' COMMENT '登录错误次数',
`isActive` tinyint(1) default '0' COMMENT '是否激活。默认为未激活。',
PRIMARY KEY (`id`),
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC
修改:
'db'=>array(
'connectionString'=>'Your DSN',
),
为:
'db'=>array(
'class' => 'CDbConnection',
'connectionString'=>'mysql:host=localhost;port=3306;dbname=center',
'username' => 'root',
'password' => '×××××××××××',
),
我们通过这个配置,连自动连接到mysql数据库。
打开默认的模型文件:
LoginForm.php,看到登录用户用到了
$identity=new UserIdentity($this->username,$this->password);
修改UserIdentity.php:
添加数据库验证部分:
private function checkUser(){
$connection = Yii::app()->db;
$connection->active = true;
$sql = "SELECT * FROM `member` WHERE `username` = '" . $this->username .
"' AND `md5Password` = '" . md5($this->password) . "'" ;
$command = $connection->createCommand($sql);
$reader=$command->query();
if ($reader->getRowCount() > 0){
return self::ERROR_NONE;
}
}
修改:
public function authenticate()
{
if(!isset($this->username)){
$this->errorCode=self::ERROR_USERNAME_INVALID;
}else{
$this->errorCode = $this->checkUser();
}
return $this->errorCode;
}
OK,现在登录,应该可以成功了吧。
表结构:
CREATE TABLE `member` (
`id` bigint(30) unsigned NOT NULL auto_increment COMMENT '自增',
`pid` int(11) NOT NULL COMMENT '用户对应的属性ID',
`username` varchar(32) NOT NULL COMMENT '用户名,可以是:用户名、email、通行证、手机号、身份证号码',
`usernameType` tinyint(5) NOT NULL COMMENT '对应member_type的ID',
`md5Password` varchar(32) NOT NULL COMMENT '加密密码',
`activeDatetime` datetime default NULL COMMENT '激活时间',
`loginErrorTimes` tinyint(5) default '0' COMMENT '登录错误次数',
`isActive` tinyint(1) default '0' COMMENT '是否激活。默认为未激活。',
PRIMARY KEY (`id`),
KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC
相关阅读 更多 +