dongtun1683 2015-12-06 19:36 采纳率: 0%
浏览 70
已采纳

如何从一个数据库中的多个表中获取多个值?

i have a serious problem!

I want to use different values from different tables in my database. My tables:

users

user_id | name | email | password | color | created

parents

parent_id | email | password

So, i want to connect these tables with email col. But passwords cols are different.

Here is my code:

        $_SESSION['logged_in2'] = false;
    if( !empty( $data ) ){

        // Trim all the incoming data:
        $trimmed_data = array_map('trim', $data);

        // escape variables for security
        $email = mysqli_real_escape_string( $this->_con,  $trimmed_data['email'] );
        $password = mysqli_real_escape_string( $this->_con,  $trimmed_data['password'] );

        if((!$email) || (!$password) ) {
            throw new Exception( PARENTS_FIELDS_MISSING );
        }
        $password = md5( $password );

        $query = "SELECT parent_id, email FROM parents where email = '$email' and password = '$password' union all SELECT name FROM users ";
        $result = mysqli_query($this->_con, $query);
        $data = mysqli_fetch_assoc($result);
        $count = mysqli_num_rows($result);
        mysqli_close($this->_con);
        if( $count == 1){
            $_SESSION = $data;
            $_SESSION['logged_in2'] = true;
            return true;
        }else{
            throw new Exception( PARENTS_FAIL );
        }
    } else{
        throw new Exception( PARENTS_FIELDS_MISSING );
    }

How can i use these tables in one code? Please help! By the way, the error is this:

mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

  • 写回答

1条回答 默认 最新

  • dongzhouzhang8696 2015-12-06 19:41
    关注

    Perhaps use a combined query(join) like this :

    select a.*, b.* from table users as a join parents as b on a.email = b.email where a.email = "$email" and a.password = "$password"
    

    updated :

    SELECT b.user_id, a.email, b.email as parent_email, a.name, a.color, b.password as parent_password FROM users AS a INNER JOIN parents AS b ON a.email = b.email WHERE b.email = '$email' AND b.password  = '$password' ;
    
    $email = mysqli_real_escape_string( $this->_con, $trimmed_data['parent_email'] );
    $password = mysqli_real_escape_string( $this->_con, $trimmed_data['parent_password'] ); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容