感觉你对类认识不够好,注意php的连接符是'.',而非'+'。:-). =========================== class user{ public $name; public $pwd; public function user($username,$password){ $this->name=$username; $this->pwd=$password; } public function check(){ mysql_select_db("php",$conn); $exec="select count(*) from yonghu where username='".$this->name."'and pwd='".$this->pwd."'"; $reslut=mysql_query($exec,$conn); if($reslut>0){ echo "用户验证通过"; } else { echo "用户名或密码错误";} } }
用时: ====================== $userChk = new user($_POST['username'],$_POST['pass']); $userChk->check();
$exec="select count(*) from yonghu where username="+$username+ "and pwd="+$password; ,首先此sql错误
===========================
class user{
public $name;
public $pwd;
public function user($username,$password){
$this->name=$username;
$this->pwd=$password;
}
public function check(){
mysql_select_db("php",$conn);
$exec="select count(*) from yonghu where username='".$this->name."'and pwd='".$this->pwd."'";
$reslut=mysql_query($exec,$conn);
if($reslut>0){
echo "用户验证通过";
}
else {
echo "用户名或密码错误";}
}
}
======================
$userChk = new user($_POST['username'],$_POST['pass']);
$userChk->check();
,首先此sql错误
if($reslut>0){
echo "用户验证通过";
}
else {
echo "用户名或密码错误";}
$result是true或false,判断条件有问题
如果查询到数据,此函数会返回一个"资源"(resource)类型.否则返回false;所以判断要这样写:
if($reslut)
echo "用户验证通过";
else
echo "用户名或密码错误";
按你那种写法也可以,不过要这样写:
if(mysql_num_rows($reslut) > 0)
echo "用户验证通过";
else
echo "用户名或密码错误";
多去看看面向对象的思想,你这样用类不如不用.mysql_connect为什么不放到Conn类里面封装起来?数据库查询应该也放到Conn类里面,数据检验用一个类,而检验的应该是数据的完整性及合理性.