我一直在尝试创建一个PHP函数,它可以使用PHP将MySQL表显示为HTML表。到目前为止,我可以输出我选择的任何表,但是当MySQL表包含空行时,我遇到了一个问题,因为空单元格会导致HTML表。我的代码是这样的:

<?php
function getTABLE(){
$db_host = 'HOST.com';
$db_user = 'USER1';
$db_pwd = 'PASSWORD';
$database = 'testdb';
$table = 'FAQTable';

 if (!mysql_connect($db_host, $db_user, $db_pwd))
     die("Can't connect to database");
 if (!mysql_select_db($database))
     die("Can't select database");

//// sending query and only result cell that are not NULL
$result = mysql_query("SELECT * FROM {$table}");

 if (!$result) {
    die("Query to show fields from table failed");
 }

$fields_num = mysql_num_fields($result);
echo "<h3><center>Table: {$table}</h3>";
echo "<table border='1'><tr>";

  //// printing table headers
 for($i=0; $i<$fields_num; $i++){
     $field = mysql_fetch_field($result);
     echo "<td>{$field->name}</td>";
 }

  //// printing table rows
  while($row = mysql_fetch_row($result)){
    echo "<tr>";
    //// $row is array... foreach( .. ) puts every element
    //// of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";
     echo "</tr>";
 }
 print "</TABLE>";
 mysql_close();
 }

 print getTABLE();

?>

我的难题是在代码的“打印表行”部分。我希望while($row = mysql_fetch_row($result))中有一种方法可以只接受其中包含值的行。有什么想法吗?
我已经试着用了以下几行,但没有成功:
$result = mysql_query("SELECT * FROM {$table} WHERE * IS NOT NULL");
$result = mysql_query("SELECT COUNT(id) FROM {$table} where answer IS NOT NULL or answer <>'' ");
$result = mysql_query('SELECT COUNT(*) FROM {$table} WHERE answer <> ""');
$result = mysql_query("SELECT * FROM {$table} WHERE CHAR_LENGTH>0");
$result = mysql_query("SELECT * FROM {$table} WHERE val1 is <> '' ");
$result = mysql_query("SELECT * FROM {$table} WHERE col1 is <> '' ");

//// Outputs funky count in a separate table, but not the desired table with no empty cells
$result = mysql_query("SELECT COUNT(answer) FROM {$table} WHERE CHAR_LENGTH(answer)>0");
$result = mysql_query("SELECT COUNT(answer) FROM {$table} WHERE LENGTH(answer)>0");

$reslts = mysql_query("SELECT * FROM {$table}");
 while($row = mysql_fetch_row($reslts)){
     $empty_count = 0;
     $count = count($row);
      for($i = 0; $i < $count; $i++)
           if($row[$i] === '' || $row[$i] === 'NULL')
              $empty_count++;
         $result = ($count);
  }

因此,感谢Paul Spiegal帮助这个PHP函数,它可以将任何MySQL表输出到HTML表中,以便在网站上显示。。。工作函数如下,只需更改变量的值即可访问任何MySQL数据:
function getTABLE(){
$db_host = 'www.host.com';
$db_user = 'user1';
$db_pwd = 'password';
$database = 'testdb';
$table = 'MyTable';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");
if (!mysql_select_db($database))
    die("Can't select database");

//// sending query
$result = mysql_query("SELECT * FROM {$table}");
 if (!$result) {
     die("Query to show fields from table failed");
 }
$fields_num = mysql_num_fields($result);

echo "<h3><center>Table: {$table}</h3>";
echo "<table border='1'><tr>";

//// printing table headers
 for($i=0; $i<$fields_num; $i++){
     $field = mysql_fetch_field($result);
     echo "<td>{$field->name}</td>";
 }

//// printing table rows
 while($row = mysql_fetch_row($result)){
    echo "<tr>";
        if (strlen(implode('', $row )) == 0) {
            continue;
       }else {
          foreach($row as $cell)
            echo "<td>$cell</td>";
            echo "</tr>";
        }
 }
print "</TABLE>";
mysql_close();
}

print getTABLE();

最佳答案

使用implode()函数,可以将所有单元格合并为一个字符串。如果该字符串为空,则跳过打印该行。

while($row = mysql_fetch_row($result)){
    if (strlen(implode('', $row)) == 0) {
        continue; // skip this empty row
    } else {
        // TODO: print this row
    }
}

关于php - 解析MySQL表中的空行并输出为html表,而没有空单元格,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36662057/

10-11 17:00