简体   繁体   English

我如何运行这些查询,以便一旦找到结果,它们就会停止

[英]How can i run these queries so they will stop once a result is found

I am trying to pull data off one table using the following queries but I need it to run the first query and stop if it gets a result and then run the second query if it doesn't. 我正在尝试使用以下查询从一个表中提取数据,但是我需要它来运行第一个查询,如果得到结果则停止,然后如果没有,则运行第二个查询。 Every time I run the search I get two results but one of the two results is echoing that there is no result. 每次运行搜索时,我都会得到两个结果,但是两个结果之一却在回声没有结果。 I plan on adding more queries as well so that the results can be returned as needed. 我还计划添加更多查询,以便可以根据需要返回结果。 I have been working on this problem for a few days already and I finally thought I would ask for help. 我已经在这个问题上工作了几天,最终我想寻求帮助。 Thanks in advance. 提前致谢。

$conn = new mysqli("localhost", "user", "password", "dbname");                                                                               
if(!$conn) {                                                                                                                                              
    die("Connection failed: " . mysqli_connect_error());                                                                                                  
}                                                                                                                                                         

//echo $conn->host_info . "\n";                                                                                                                           

$sql = "SELECT * FROM SCANMSTR WHERE BadgeNumber LIKE $query AND Active = 1 AND ExpirationDate >= curdate()";                                             
$result = mysqli_query($conn, $sql);                                                                                                                      

if (mysqli_num_rows($result) > 0) {                                                                                                                       
   while($row = mysqli_fetch_assoc($result)) {                                                                                                            
        echo "Badge Number: " .$row["BadgeNumber"]. "- Name: " . $row["BadgeName"]. "<br>";                                                               
     }                                                                                                                                                    
}                                                                                                                                                         

else  {                                                                                                                                                   
  echo "Not in system1";                                                                                                                                  
}                                                                                                                                                         


$sql1 = "SELECT * FROM SCANMSTR WHERE BadgeNumber LIKE $query and Active = 0 AND Barred = 1 AND Lost = 1";                                                
$result1 = mysqli_query($conn, $sql1);                                                                                                                    

if (mysqli_num_rows($result1) >0) {                                                                                                                       
   while($row1 = mysqli_fetch_assoc($result1)) {                                                                                                          
        echo "Card not active";  }                                                                                                                        
      }                                                                                                                                                   

else {                                                                                                                                                    
   echo "Not in system";

//Newly added code //新添加的代码

$row = mysqli_fetch_assoc($result); $ row = mysqli_fetch_assoc($ result);
if ($row) { 如果($ row){
if ($row['Active']) { 如果($ row ['Active']){
echo "Badge Number: " .$row["BadgeNumber"]. 回显“徽章编号:”。$ row [“ BadgeNumber”]。 "- Name: ".$row["BadgeName"]. “-名称:”。$ row [“ BadgeName”]。 "
"; “;
} }
else { 其他{
echo "Card is inactive"; 回显“卡无效”;
} }
if ($row) { 如果($ row){
if($row['Barred']) 如果($行[ '禁止的'])
echo "Cardholder is barred"; 回显“持卡人被禁止”;
} }
if ($row) { 如果($ row){
if($row['Lost']) 如果($行[ '迷失'])
echo "Card reported Lost"; 回显“卡报告丢失”;
} }
} }
else { 其他{
echo "Not in system"; 回显“不在系统中”;

You put in your second statement into the first ELSE of your first statement like this: 您将第二条语句放入第一条语句的第一条ELSE中,如下所示:

$conn = new mysqli("localhost", "user", "password", "dbname");
if(!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

//echo $conn->host_info . "\n";

$sql = "SELECT * FROM SCANMSTR WHERE BadgeNumber LIKE $query AND Active = 1 AND ExpirationDate >= curdate()";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
   while($row = mysqli_fetch_assoc($result)) {
        echo "Badge Number: " .$row["BadgeNumber"]. "- Name: " . $row["BadgeName"]. "<br>";
     }
}

else  {                                                                                                                                                   
$sql1 = "SELECT * FROM SCANMSTR WHERE BadgeNumber LIKE $query and Active = 0 AND Barred = 1 AND Lost = 1";
$result1 = mysqli_query($conn, $sql1);

if (mysqli_num_rows($result1) >0) {
   while($row1 = mysqli_fetch_assoc($result1)) {
        echo "Card not active";  }
      }

else {
   echo "Not in system";}

 }

You could perhaps put both queries in one, and only fetch the first row found? 您也许可以将两个查询都放在一个查询中,而只获取找到的第一行? Something like this: 像这样:

$sql = "SELECT BadgeNumber, BadgeName, Active FROM SCANMSTR WHERE BadgeNumber LIKE $query AND Active = 1 AND ExpirationDate >= CURDATE()
        UNION
        SELECT BadgeNumber, BadgeName, Active FROM SCANMSTR WHERE BadgeNumber LIKE $query and Active = 0 AND Barred = 1 AND Lost = 1
        LIMIT 1";

$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row) {
    if ($row['Active']) {
        echo "Badge Number: " .$row["BadgeNumber"]. "- Name: " . $row["BadgeName"]. "<br>";
    }
    else {
        echo "Card not active";  
    }
}
else {
    echo "Not in system";
}

Edit To extend this to N checks you can either add more 编辑要将其扩展到N个支票,您可以添加更多

UNION
SELECT ...

lines to $sql, like 到$ sql的行,例如

$sql = "SELECT BadgeNumber, BadgeName, Active FROM SCANMSTR WHERE BadgeNumber LIKE $query AND Active = 1 AND ExpirationDate >= CURDATE()
        UNION
        SELECT BadgeNumber, BadgeName, Active FROM SCANMSTR WHERE BadgeNumber LIKE $query and Active = 0 AND Barred = 1 AND Lost = 1
        UNION
        SELECT ...
        UNION
        SELECT ...
        ...
        LIMIT 1";    

Because of the LIMIT 1 the query will check as many of the SELECT queries as needed to get one row to return. 由于LIMIT 1,查询将根据需要检查尽可能多的SELECT查询以返回一行。 So even if multiple SELECT matches the person(not sure if it is/should be possible?) you'll get the first of them, not all. 因此,即使多个SELECT匹配该人(不确定是否/应该吗?),您也会得到第一个,而不是全部。

Or you could simply fetch the columns needed for all the checks, and then do them in your backend logic: 或者,您可以简单地获取所有检查所需的列,然后在后端逻辑中进行处理:

$sql = "SELECT BadgeNumber, BadgeName, Active, ExpirationDate 
        FROM SCANMSTR 
        WHERE BadgeNumber LIKE $query
        LIMIT 1";

$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if ($row) {
    if ($row['Active']) {
        echo "Badge Number: " .$row["BadgeNumber"]. "- Name: " . $row["BadgeName"]. "<br>";
    }
    else if ($row['ExpirationDate'] < date('Y-m-d')) {
        echo "Card not active";
    }
}
else {
    echo "Not in system";
}

Personally I think I'd do it this way, it feels more clean, less repeated SQL, etc. Both ways should work, though :).. 我个人认为我会这样做,感觉更干净,重复SQL更少,等等。尽管这两种方法都应该可行,但:) ..

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

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