开发者

php/mysql checking for empty/null fields and outputing it on a modal page

This script here by GeekFish can get you empty/null fields in a table. It however outputs the results in an array format and repeats itself. Eg. Array ( [0] => Field "5" on entry "1" is empty/null [1] => Field "price" on 开发者_如何转开发entry "1" is empty/null. I only need the results in the odd number arrays [1],[3],[5].. and print them to a modal form. Thanks.

$sql = "SELECT * FROM TABLE_PRODUCTS";
  $res = mysql_query($sql);
   $emptyFields = array();
 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['table_primary_key']);
    }
 }
}print_r($emptyFields);


Thats not really a good script, why not only return data that has fields that are empty or null in the SQL statement?

Also, what do you mean by only odd rows? do you mean you want to skip every other row? if so this 'should' work:

   <?php
    $sql = "SELECT * FROM TABLE_PRODUCTS";
      $res = mysql_query($sql);
       $emptyFields = array();
     $row_count=1;
     while ($row = mysql_fetch_array($res)) {
      if(($count%2) != 0) {
        foreach($row as $key => $field) {
          if(empty($field)) {
            $emptyFields[] = sprintf('Field "%s" on entry "%d" is empty/null', $key,  $row['table_primary_key']);
          }
       }
       $row_count++;
     }
    }print_r($emptyFields);

let me know if you meant something else

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜