简体   繁体   English

如何在单个sql查询中获取失败的条件

[英]How to get which condition has failed if any in a single sql query

This might be odd or not... but I need to find out which condition out of 3 has failed if any... 这可能是奇怪的,也可能不是。。。但是我需要找出3个条件中有哪个失败了...
I explain... 我解释...
In my query I have 3 condition if all goes fine then no problem, but if one of those conditions fail I need to know which condition or conditions have fail so that I can build a proper response... 在我的查询中,如果所有条件都正常,那么我有3个条件,但是没有问题,但是如果其中一个条件失败,则我需要知道哪个条件或多个条件失败,以便我可以建立适当的响应...
Example: 例:

if(strlen($k) == 16 && filter_var($em, FILTER_VALIDATE_EMAIL)) {
        $con = $c->Con();
        $q = $con->query("SELECT COUNT(*) FROM table WHERE v = '$k' AND ema = '$em' AND ve = '0'");
        $r = $q->fetch_row();
        if($r > 0) { $q = $con->query("UPDATE table SET ve = '1' WHERE v = '$key' AND emails = '$em'");
            if($q){
                $mss = 'Ready'; // MS 1
            }
        } else {
            $mss = 'It was ready'; // MS 2
        }
    } else {
        $mss = 'Something is wrong!'; // MS 3
    }

So the first query has 3 conditions if all are ok then go to the next "query", BUT, how do I know is one of the conditions fail?... what if [v] is not a match, or if the email is not a match or if ve is not a match... that way I can show a proper message on the MS 2.... instead of a general message... 所以第一个查询有3个条件,如果一切都OK,然后转到下一个“查询”,但是,我怎么知道其中一个条件失败?...如果[v]不匹配,或者如果电子邮件,该怎么办?不匹配或ve不匹配...那样我可以在MS 2上显示正确的消息...而不是一般消息...
if v is not equal to $k then $mss = 'Your key is not a match'; 如果v不等于$ k,则$ mss ='您的密钥不匹配';
if emails is not equal to $ema then $mss = 'Your email was not fund'; 如果电子邮件不等于$ ema,则$ mss ='您的电子邮件没有资金';
if ve is not equal to 0 then $mss = 'Your key has no value'; 如果ve不等于0,则$ mss ='您的密钥没有值';

the thing is that I would like to keep it as short and clean as possible, I can do a separate query for each one of the conditions which is a lazy way to do it but effective... 问题是我想使其尽可能短而整洁,我可以针对每个条件进行单独的查询,这是一种懒惰的方法,但是有效...

I can come up with a clunky solution that mostly shifts the work to PHP. 我可以提出一个笨拙的解决方案,主要将工作转移到PHP。

  1. Change you query to SELECT the rows using ORs. 更改查询以使用OR选择行。 To return the set of possible rows. 返回可能的行集。

SELECT v, ema, ve FROM table WHERE v = '$k' OR ema = '$em' OR ve = '0';

  1. Now go through this result set to check the conditions. 现在,通过此结果集检查条件。

Pseduocode 伪码

while( $row = fetch_row($result) ){
  if( 1 and 2 and 3 ){
    //SUCCESS
  }
  elseif( NOT 1 ){
    //1 is the problem
  }
  elseif( NOT 2 ){
    //2 is the problem
  }
  elseif( NOT 3 ){
    //3 is the problem
  }
}

1,2,3 are the condition (eg 1 = ($row['v'] == $k)) 1,2,3是条件(例如1 =($ row ['v'] == $ k))

You'll have to figure out what to do about multiple rows, ideally the schema would prevent that. 您必须弄清楚如何处理多行,理想情况下,架构可以避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM