tags:

views:

39

answers:

2

i want to check if a column contains any fieldvalues in a table.

cause i want to get the fieldvalues in a column and do something with them, but first i have to check whether they exist or not.

i have tried:

$query = "SELECT anonymous_username FROM users";
$result = mysqli_query($conn, $query) or die ("Couldn't execute query: " . mysqli_error($conn));

if(mysqli_num_rows($result) > 0)
{
 while($row = mysqli_fetch_assoc($result))
 {
  echo "exists";
 }
}

but even when all the fields for that column were empty it echoed back 'exists'. so how can i do it?

EDIT: google is your friend:

 $query = "SELECT anonymous_username FROM users WHERE anonymous_username <> '' AND anonymous_username IS NOT NULL";
+3  A: 

mysqli_affected_rows is only for queries that change data like INSERT, UPDATE, REPLACE or DELETE. For SELECT use mysqli_num_rows.

Gumbo
but it still didnt helped because the result is still 1. it gets the field although its empty. basically i want to check if any fields in one column has a value or not.
weng
A: 

If you're looking to check if any row has a non-NULL, non-empty anonymous_username column, you could:

SELECT count(*) as Num
FROM users
WHERE LENGTH(anonymous_username) > 0

In PHP, you can retrieve the number like:

$result = mysql_query($sql);
$row = mysql_fetch_assoc( $result );
$rowcount = $row['num'];
Andomar