What I mean is, for example, if I search for something on an SQL table, and it returns 0 / nothing, is that null or empty or does it result in an empty “”?
I’m getting back into php and trying to loop through a db like so:
Pseudo code:
while($row = mysqli_fetch_array($var here that has the query to the db and connection)){
echo $row[//something pulled from table to be read back]
}
But let’s say that the search query didn’t return any results or that $row doesn’t equal to anything because for whatever reason, the value searched for in the db doesn’t exist,
How then can I check for the empty-ness of row?
if the searched for item doesn’t exist in the db, then $rows value becomes what exactly? null/empty/or empty string or something else?
depending on the value of row, do I test for [isset / !isset] or [empty / !empty] ? Or is the only way to test for empty $row to do a (pseudo) $x =mysqli_num_rows(var here) / if ($x == 0 ) //do something?
If
mysqli_fetch_arraydid not return anything, then the return value isNULLThe line
while($row = mysqli_fetch_arrayis supposed to return the next available record in the record set, so if the query did not give any matching results, then the lineecho $row['something']does not get executed at all.If there are multiple fields you are fetching, say
field1,field2and supposingfield2doesn’t have a value, then what is returned for that field is what the default value for that field will be in MySQL (or whichever DB you are using). However the point to note is that$rowwill still have two keys defined correctly as$row['field1']and$row['field2']If you want to check if
$row['field2']does have any value you can check it usingempty($row['field2'])."", 0, NULL, FALSE, array()are all consideredempty()so you should be able to safely determine if the field has any valid value in it using this function. However, if say 0 could be a valid value (for e.g. you are querying no_of_days a person was absent last month and some of them have full attendance), you will need to explicitly check the value in the field and cannot depend on empty().