Home > Back-end >  Can I check which condition after where was true and got executed in my query?
Can I check which condition after where was true and got executed in my query?

Time:10-01

I have a get parameter and I want to select from my table based on this get parameter, But I have to check the two conditions after where if the first was not equal then move to the next one. But the main problem is I want to know which condition was executed the first one or the second one.

$sql = $db_->prepare("Select * from table_x where url_en = ? Or url_fr = ?"); 

Instead of doing this:

    $sql = $DB_->prepare("select * from tbl_items where `url_ar`= ?");
       $sql->bind_param("s", $_GET["url"]);
       $sql->execute();
       $result = $sql->get_result();
       if($result->num_rows == true){$lang = "ar";}else{
           
    $sql = $DB_->prepare("select * from tbl_items where `url_fr`= ?");
           $sql->bind_param("s", $_GET["url"]);
           $sql->execute();
           $result = $sql->get_result();
           if($result->num_rows == true){$lang = "fr";}else{
               
               echo "unfound";exit;
               
           }
       }

I want something short by using just SQL

CodePudding user response:

Try searching up each value independently or try bringing them up together in a list and see where they reside when trying to organize it with ORDER BY.

CodePudding user response:

You can use UNION and execute each condition separately. Using some kind of label you can then find out which condition was true.

SELECT 'EN' as label, * FROM table_x WHERE url_en = ?
UNION
SELECT 'FR' as label, * FROM table_x WHERE url_fr = ?
  • Related