Check if MySQL row is empty

Hello,

I have several columns in a MySQL row and I’m wondering if there quick function to check if any thing is in the columns or if they are all empty?

Thanks


if (empty($row)) {
  ...
}

That won’t work since even if every column is empty, $row will be an array of empty elements and therefore not empty.

Are you looking for a MySQL function or a PHP function? In other words, have you already fetched the row from the server?

To do it in mysql:


SELECT (column1 IS NULL AND column2 IS NULL AND column3 IS NULL [etc...]) as isempty FROM your_table WHERE [where statement])

This will give you a column called ‘isempty’ with a boolean value telling you if the row is empty or not.

If you have already fetched the row to an array, just check if each value is empty:


function isRowEmpty($row){
foreach($row as $a){
if(!empty($a)){
return false;
}
}
return true;
}