how to write if condition for multpile queries like

48 Views Asked by At

I have 3 queries like

$q1 = select email from tbl students
$q2 = select email from tbl corporates
$q3 = select email from tbl institutes

i want check to all 3 tables if email count is zero from all above then only do proceed like that,, can i write like this:

if(mysqli_num_rows($q1) || mysqli_num_rows($q2)  || mysqli_num_rows($q3) ==0){
proceed to process. 
}else{
do nothing
}

My question is it a correct way to approach or anything is better left which i need to learn.

3

There are 3 best solutions below

0
On BEST ANSWER

you need to use & instead of || and also check rows count for each of your mysql object

if(mysqli_num_rows($q1) == 0 && mysqli_num_rows($q2) == 0 && mysqli_num_rows($q3) ==0){
    //proceed to process. 
} else {
    //do nothing
}
0
On

try can do easier using mysqli_multi_query()

$con=mysqli_connect("localhost","my_user","my_password","my_db");// Check connection

if (mysqli_connect_errno()) {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();   

}

$sql = select email from tbl students";
$sql .= select email from tbl corporates";
$sql .= "select email from tbl institutes";   

// Execute multi query   

if (mysqli_multi_query($con,$sql)){     

do{   // Store first result set   

    if ($result=mysqli_store_result($con)) {  // Fetch one and one row

       if (mysqli_fetch_row($result) == 0)  {    

           //your code here
    }  
 }  

}

0
On

You should execute those queries first so you will have:

$db = mysqli_connect("localhost","my_user","my_password","my_db");
$q1 = "select email from tbl students";
$q2 = "select email from tbl corporates";
$q3 = "select email from tbl institutes";

$db_q1 = mysqli_query($db, $q1);
$db_q2 = mysqli_query($db, $q2);
$db_q3 = mysqli_query($db, $q3);

if(mysqli_num_rows($db_q1) == 0 && mysqli_num_rows($db_q2) == 0 && mysqli_num_rows($db_q3) == 0) {
proceed to process. 
}