简体   繁体   中英

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);
if ($row) {
if ($row['Active']) {
echo "Badge Number: " .$row["BadgeNumber"]. "- Name: ".$row["BadgeName"]. "
";
}
else {
echo "Card is inactive";
}
if ($row) {
if($row['Barred'])
echo "Cardholder is barred";
}
if ($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:

$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

UNION
SELECT ...

lines to $sql, like

$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. 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.

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 :)..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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