如果内容不在一个表中,请检查另一个表

gg58donl  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(276)

我正在尝试设置if/else大小写,以查看某个值是否在一个表中,然后显示所述值,如果该值不在一个表中,则检查另一个表。
我正在Cloud9中使用php和mysql。电子邮件是从未显示的表单获取的。
使用正确的电子邮件搜索“customer”表会显示密码\enc,但是如果在customer表中找不到该电子邮件,并且它会搜索“company”表,则不会显示任何内容。我查了phpmyadmin,邮件是存在的,有效的
我也检查了我的表格,“email”和“rep\u email”是有效的。
有没有更简单的方法来实现这一点,或者我当前的代码需要调整?谢谢你的帮助。

$email              = !empty($_POST ['email']) ? $_POST['email'] : "";
$email2             = $email;

$query  = "SELECT * FROM Customer WHERE email = '$email'";
$result = $db->query($query);

if ($result){
    $row            = $result->fetch_assoc(); /* These lines query the dataabse when user enters email */
    $password_enc   = $row['password_enc'];
}
elseif(!$result){
    $query2         = "SELECT * FROM Company WHERE rep_email = '$email2'";
    $result2        = $db->query($query2);
    $row2           = $result2->fetch_assoc(); /* These lines query the dataabse when user enters email */
    $password_enc   = $row2['password_enc'];
}

echo "password_enc: ".$password_enc;
fykwrbwg

fykwrbwg1#

$email = !empty($_POST ['email']) ? $_POST['email'] : "";

$query = "SELECT result.* FROM( SELECT IFNULL((SELECT password_enc FROM Customer WHERE email = '$email'), (SELECT password_enc FROM Company WHERE rep_email = '$email')) as password_enc) as result";

$result = $db->query($query);
$password_enc = '';

if ($result->num_rows > 0){
    $row            = $result->fetch_assoc();
    $password_enc   = $row['password_enc'];
}

echo "password_enc: ".$password_enc;
u7up0aaq

u7up0aaq2#

如果选择、显示、描述或解释查询成功,mysqli\u query()将返回mysqli\u result对象
php mysqli手册
因此,不必检查响应,实际上可以检查返回的行数,如下所示

$query  = "SELECT * FROM Customer WHERE email = '$email'";
$result = $db->query($query);

if ($result->num_rows > 0){
   $row            = $result->fetch_assoc(); /* These lines query the dataabse when user enters email */
   $password_enc   = $row['password_enc'];
}else{
   $query2         = "SELECT * FROM Company WHERE rep_email = '$email2'";
   $result2        = $db->query($query2);
   if($result2->num_rows>0){
      $row2           = $result2->fetch_assoc(); /* These lines query the dataabse when user enters email */
      $password_enc   = $row2['password_enc'];
   }
}

if(isset($password_enc)){
   echo "password_enc: ".$password_enc;
}else{
   echo "Password Not Available";
}
g6ll5ycj

g6ll5ycj3#

你应该使用 num_rows 从结果集中检索行数。

$email              = !empty($_POST ['email']) ? $_POST['email'] : "";
$email2             = $email;

$query  = "SELECT * FROM Customer WHERE email = '$email'";
$result = $db->query($query);

if ($result->num_rows > 0){ //check if results is bigger then 0 
    $row            = $result->fetch_assoc(); /* These lines query the dataabse when user enters email */
    $password_enc   = $row['password_enc'];
} else {
    $query2         = "SELECT * FROM Company WHERE rep_email = '$email2'";
    $result2        = $db->query($query2);
    $row2           = $result2->fetch_assoc(); /* These lines query the dataabse when user enters email */
    $password_enc   = $row2['password_enc'];
}

echo "password_enc: ".$password_enc;

相关问题