这个登录函数,除了要查找数据库,寻找匹配外,还要判定是在哪张表中找到的匹配,并标记下来,最后返回这个标记。
我在另一个脚本中调用这个函数,但是频频丢出异常,我也实在看不出来,到底是哪里的问题。
请大侠们帮忙检查一下,谢谢!
function login($username, $password) {
$conn = db_connect();
// check if username is unique
$result = $conn->query("select * from admin
where username='".$username."'
and password = sha1('".$password."')");
if (!$result) {
$result = $conn->query("select * from customers
where username='".$username."'
and password = sha1('".$password."')");
if (!$result) {
throw new Exception('Could not log you in.');
} else {
$type = 'customer';
}
} else {
$type = 'admin';
}
if ($result->num_rows>0) {
return $type;
} else {
throw new Exception('Could not log you in.');
}
}
我在另一个脚本中调用这个函数,但是频频丢出异常,我也实在看不出来,到底是哪里的问题。
请大侠们帮忙检查一下,谢谢!
function login($username, $password) {
$conn = db_connect();
// check if username is unique
$result = $conn->query("select * from admin
where username='".$username."'
and password = sha1('".$password."')");
if (!$result) {
$result = $conn->query("select * from customers
where username='".$username."'
and password = sha1('".$password."')");
if (!$result) {
throw new Exception('Could not log you in.');
} else {
$type = 'customer';
}
} else {
$type = 'admin';
}
if ($result->num_rows>0) {
return $type;
} else {
throw new Exception('Could not log you in.');
}
}