检查PHP和MySQL中的所有空列

检查PHP和MySQL中的所有空列

问题描述:

Ex : I have 1 table below

ID   USER1    USER2     USER3
1      X                  X
2      X                  X
3      X                  X
4      X                  X
5      X                  X

How i can check ALL value USER2 comlumn is empty in PHP & MySQL? I code below but it's not working

$res = mysqli_query($conn, "SELECT USER2 FROM TABLE")
if(count($res)== "0") echo "OK";

例如:我有1个表 p>

  ID USER1 USER2  USER3 
1 XX 
2 XX 
3 XX 
4 XX 
5 XX 
  code>  pre> 
 
 

如何检查所有值USER2 comlumn在PHP& MySQL的? 我在下面编写代码,但它不起作用 p>

  $ res = mysqli_query($ conn,“SELECT USER2 FROM TABLE”)
if(count($ res)==“0”)  echo“OK”; 
  code>  pre> 
  div>

$res = mysqli_query($conn, "SELECT * FROM TABLE where USER2=''")
$n_rows_void_user2 = mysql_num_rows($res);
if($n_rows_void_user2) echo "OK";
else
echo "There are $n_rows_void_user2";

you can do something like this:

    $res = mysqli_query($conn, "SELECT * FROM TABLE where USER2 IS NULL OR USER2 = ''");

    $num = $res->num_rows;
   if($num != 0){
        echo "number of empty row for USER2".$num;
       }else{
     echo "on empty column for USER2";
    }

select sum(user2 <> '') as count
from your_table

First of all you have to write the correct query:

"SELECT * FROM mytable WHERE --user2 condition--"

Where user2 condition may be different depend on your data:

WHERE ISNULL(user2) OR user2=''

It depends on your data.

Then to execute this query in php:

$res = mysqli_query($conn, $query);
$n_rows = mysql_num_rows($res);
if ($n_rows > 0) {
  // echo your rows here
}

Also you should connect to database before, i hope you did that.

This would help,

$res = mysqli_query($conn, "SELECT COUNT(*) AS count FROM TABLE where USER2 IS NULL OR USER2=''");
if($res['count']==0) 
{
  echo "It is Empty";
}
else
{
   echo $res['count']." rows have User2 empty";
}

count(*) returns the no of rows coming as the output of the query, which will be in integer form. $res == "0" means that you are comparing with a string "0", not an integer.

An extension to the answer by @juergen :

   select sum(user2 IS NOT NULL OR some_col <> '') as count
    from your_tabl;