7

This code loops through a mysql table and prints out empty/null fields. It however prints the array values and the keys like this

Array ( 
    [0] => Field "dob" on entry "1" is empty/null 
    [1] => Field "user_name" on entry "7" is empty/null
)

How do I print something like this field "dob" on entry "1" is empty/null

$sql = "SELECT * FROM userinfo";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res)) {
    foreach($row as $key => $field) {
        if(empty($field)) {
            $emptyFields[] = sprintf('Field "%s" on entry "%d" is empty/null', $key,   $row['userid']);
         }
     }
}
print_r($emptyFields);
0

4 Answers 4

9
echo implode('<br>', $emptyFields);
Sign up to request clarification or add additional context in comments.

3 Comments

it worked fine. It prints out Field "4" on entry "11" is empty/null Field "address" on entry "11" is empty/null. They are all the same. How do i avoid printing field "4" on entry "11" is empty/null.
I dont understand what do you need. You want to skip integer keys, am I right? Just use check if( ! is_integer($key) AND empty($field)).
How do i avoid printing column headers repeatedly? eg. Field "name" on column "2" is empty, Field "name" on column "3" is empty. I just wanna print one field "name" and give details rather on a separate page.
1

That is because you are using print_r to output that array. So the output is formatted readable for humans. To make it more pretty try to iterate through it like you did before with that field:

foreach($emptyFields as $key => $field) {
echo('Field "'.$field.'" on entry "'.$emptyField['userid'].'" is empty/null');
}

Comments

0
$sql = "SELECT * FROM userinfo";
$res = mysql_query($sql);
while ($row = mysql_fetch_array($res)) {
  foreach($row as $key => $field) {
    if(empty($field)) {
      $emptyFields[] = sprintf('Field "%s" on entry "%d" is empty/null', $key,  $row['userid']);
    }
  }
}

Comments

0

i dont know if I get this correctly but I think the solution to your problem is:

change the last line to:

$emptyFields[] = sprintf('Field "%s" on entry "%d" is empty/null', $field, $row['userid']);}}}print_r($emptyFields);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.