I am having a problem with the following query that should count Null and Not Empty records in database. I need to use a prepared statement to execute. I have the following code but I cant get the correct output. Any help would be greatly appreciated!
$query = "SELECT UserName, COUNT(NULLIF(TRIM(UserName), ''))
FROM Employee";
$stmt = $db->prepare($query7);
$stmt->execute();
$stmt->store_result();
$numrows = $stmt->num_rows;
$stmt->bind_result($Count);
for ($i=0; $i <$numrows; $i++) {
$stmt->fetch();
echo "Count: $Count";
};
UserName? That will just pick an arbitrary username from the table. Why do you use a loop to print the results, since there's just one row with the total for the entire table. Did you mean to group the data in some way?$result=mysql_query("SELECT SUM(UserName IS NOT NULL OR TRIM(UserName) != '') FROM Employee"); $Count=mysql_fetch_assoc($result); echo "Total: $Count";