如果第一列为空,如何选择另一列 我有拥有的用户表
email
phone
和其他数据
我有用php构建的注册表
我想要的是检查用户是否插入电子邮件或电话
如果他插入电子邮件和电话
检查电子邮件是否在MySQL DB中
如果他没有在表格中插入电子邮件,而仅插入电话号码
我想检查电话号码是否在MySQL数据库中
我要说的就是这个流程图:
select email, phone where email =$email
if email is null
select phone where phone =$phone
else
select email where email = $email
请注意,必须填写表格中的电话号码 因此电子邮件和电话都不能为空
我尝试过 如果陈述 合并() 但我总是没有得到我所期望的num_rows
$chk_query = " SELECT email,phone FROM users WHERE email = '$email' IF (email IS NULL)
SELECT phone WHERE phone = '$phone' ";
$result = $mysqli->query($chk_query);
if ($result->num_rows > 0 ){
$_SESSION['message'] = 'The Email or Phone number you enter it is exist ! ';
}
else{
// insert data to the database after checking
答案 0 :(得分:0)
可变的电子邮件或电话的值已设置
$check_with ='';
if(isset($email) && $email!=null){
$check_with =$email;
}
if(isset($phone) && $phone!=null){
$check_with =$phone;
}
在查询中,检查电子邮件或电话是否存在。
$chk_query = 'SELECT email,phone FROM users WHERE phone = "'.$check_with.'" OR email = "'.$check_with.'"';
第二种方式:
根据值更改查询。
if(isset($email) && $email!=null){
$chk_query = 'SELECT email,phone FROM users WHERE email = "'.$email.'"';
}
if(isset($phone) && $phone!=null){
$chk_query = 'SELECT email,phone FROM users WHERE phone = "'.$phone.'"';
}
答案 1 :(得分:0)
根据一个问题,我有两种选择-如果电子邮件和电话都不为空,并且数据库中存在电话,您是否要引发错误或输入新数据?
如果您始终要检查电子邮件或电话是否存在,那么可以使用OR吗?
controlInstance.Anchor = AnchorStyles.Top | AnchorStyles.Right;
如果要在输入电子邮件后忽略电话,请添加一个附加条件(仅返回第一个非空列
$chk_query = " SELECT coalesce(email,phone) FROM users WHERE email = '$email' OR phone = '$phone' ";
$result = $mysqli->query($chk_query);
if ($result->num_rows > 0 ){
$_SESSION['message'] = 'The Email or Phone number already exists';
}
else{
// insert data to the database after checking
合并将在列列表中选择第一个非空值。请注意,这仅在值真正为null时有效,而在它们为空字符串时则无效。