如何从一个数据库中的多个表中获取多个值?
How to get multiple values from multiple tables in one db?
我有一个严重的问题!
我想使用数据库中不同表的不同值。我的桌子:
用户
user_id |姓名 |电邮 |密码 |颜色 |已创建
parents
parent_id |电邮 |密码
所以,我想将这些表格与电子邮件列联系起来。但是密码列不同。
这是我的代码:
$_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 );
}
如何在一个代码中使用这些表?请帮忙!对了,错误是这样的:
mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,布尔值给定
mysqli_num_rows() 期望参数 1 为 mysqli_result,布尔值给定
也许使用这样的组合查询(连接):
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"
已更新:
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'] );
我有一个严重的问题!
我想使用数据库中不同表的不同值。我的桌子:
用户
user_id |姓名 |电邮 |密码 |颜色 |已创建
parents
parent_id |电邮 |密码
所以,我想将这些表格与电子邮件列联系起来。但是密码列不同。
这是我的代码:
$_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 );
}
如何在一个代码中使用这些表?请帮忙!对了,错误是这样的:
mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,布尔值给定 mysqli_num_rows() 期望参数 1 为 mysqli_result,布尔值给定
也许使用这样的组合查询(连接):
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"
已更新:
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'] );