php内连接超过2个表

时间:2016-09-03 23:20:44

标签: php

我有2个表,一个用于登录ID,用户名,密码和等级,另一个用于帐户状态。我需要加入表来只执行所有活动帐户。每次我在phpmyadmin中尝试我的代码它都可以正常工作,但在我的系统中它将无法正常工作。我的代码有什么问题吗?顺便说一句。如果您是管理员或学生,则排名用于帐户验证。

$sql = "SELECT a.login_id,username,password,rank,b.status FROM login2 as a    INNER JOIN login_stat2 as b ON a.login_id = b.login_id WHERE username     ='".$username."' AND password='".$pass."' AND b.status='ACTIVE'";

$result = mysqli_query($con,$sql)or die(mysqli_error($con));
while($row = mysqli_num_rows($result));
session_regenerate_id();
        $member = mysqli_fetch_assoc($result);
        $_SESSION['SESS_MEMBER_ID'] = $member['login_id'];
        $_SESSION['SESS_NAME']      = $member['username'];
        $_SESSION['SESS_RANK']      = $member['rank'];


        if($member['rank']=='administrator'){
        header("location: login_reg.php");
        }

        else if($member['rank']=='student'){
        header("location: student.php");
        }
        else if($member['rank']=='candidate'){
        header("location: candidate.php");
        }

        }else{
        print "Incorrect username or password";

            session_write_close();

1 个答案:

答案 0 :(得分:0)

$sql = "SELECT a.login_id,a.username,a.password,a.rank,b.status FROM login2 as a INNER JOIN login_stat2 as b ON a.login_id = b.login_id WHERE a.username     ='".$username."' AND a.password='".$pass."' AND b.status='ACTIVE'";

$result = mysqli_query($con,$sql)or die(mysqli_error($con));

if(mysqli_num_rows($result)) {
   echo 'Login success';
} else {
   echo 'invalid username and password';
}

mysqli_num_rows()函数返回结果集中的行数。

更新代码

//create connection
$conn = new mysqli('localhost', 'root', '', 'demo');

//check connection
if($conn->connect_error) {
  die("connection failed: ".$conn->connect_error);
}
echo "Connection Successful <br>";

$sql = "SELECT a.login_id,a.username,a.password,a.rank,b.status FROM login2 as a INNER JOIN login_stat2 as b ON a.login_id = b.login_id WHERE a.username ='admin' AND a.password='admin' AND b.status='ACTIVE'";

$result = mysqli_query($conn,$sql) or die(mysqli_error($con));
if(mysqli_num_rows($result)) {
    echo "login";
} else {
    print "Incorrect username or password";
}