如何为多个查询编写 if 条件,例如

how to write if condition for multpile queries like

我有 3 个类似的查询

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

我想检查所有 3 个表,如果上面所有的电子邮件计数为零,那么只这样做, 我可以这样写吗:

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

我的问题是这是一种正确的方法还是我需要学习的更好的东西。

您需要使用 & 而不是 ||并检查每个 mysql 对象的行数

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

您应该首先执行这些查询,这样您将拥有:

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

尝试使用 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
    }  
 }  

}