无法仅选择包含数据的列

时间:2015-05-18 23:15:37

标签: php html mysql

$a = 0;
$b = 0;
$array = [];
while ($a < mysql_num_fields($result)) {
    $meta = mysql_fetch_field($result, $a);
    if (!$meta) {
        //error
    }
    while($row = mysql_fetch_row($result))
{
    foreach($row as $cell)
    $array[$b] = $cell;
    $b++;
}
    }
    $a++;
    $getColumns = mysql_query("SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='OCN' AND `TABLE_NAME`='cpu'") or die(mysql_error());
$emptyColumns = array();

while($columns = mysql_fetch_assoc($getColumns)){
    $checkEmptyColumn = mysql_query("SELECT IF(". $columns['COLUMN_NAME'] ." IS NULL or ". $columns['COLUMN_NAME'] ." = '', 'empty', ". $columns['COLUMN_NAME'] .") as ". $columns['COLUMN_NAME'] ." from cpu") or die(mysql_error());
    $numEmptyColum = mysql_num_rows($checkEmptyColumn);
    if($numEmptyColum > 0){
        while($test = mysql_fetch_assoc($checkEmptyColumn)){
            array_push($emptyColumns, $test[$columns['COLUMN_NAME']]);
            //$emptyColumns[$columns['COLUMN_NAME']] = $test[$columns['COLUMN_NAME']];
        }
    }
}

echo "<pre>";
print_r($emptyColumns);
echo "</pre>";

我正在尝试仅选择其中至少包含一个条目的列。如果整个列为NULL或为空,我不想显示它。不幸的是,我不知道会出现什么列名。我想当前的方法可以拉出每个列的名称,并通过循环插入它。它仍然打印完全为null的列。

我做错了什么?

2 个答案:

答案 0 :(得分:1)

经过长时间的聊天,终于明白了他想要的是什么,我已经修改了我的答案。

基本上他想要做的是只显示其中一行中包含数据的表列。因此,如果所有现有行中的列都为空,则不会显示。

使用PHP和MySQL执行此操作可能会耗费大量的cpu处理时间,因为他有超过500列。因此我用jQuery完成了同样的结果:

<!DOCTYPE HTML>
<html>
    <head>
        <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
        <title>OCN CPU PPD Database</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script>
        $(document).ready(function(){
            $('#main th').each(function(i) {
                var remove = 0;

                var tds = $(this).parents('table').find('tr td:nth-child(' + (i + 1) + ')')
                tds.each(function(j) { if (this.innerHTML == '') remove++; });

                if (remove == ($('#main tr').length - 1)) {
                    $(this).hide();
                    tds.hide();
                }
            });
        });
        </script>
    </head>
    <body>
        <h1 style="text-align: center;">Overclock.net CPU PPD Database</h1>
        <table id="main">
            <tr class="header">
                <?php

                $conn = mysql_connect('localhost', 'user', 'password');
                if (!$conn) {
                    die('Could not connect: ' . mysql_error());
                }
                mysql_select_db('OCN');

                $getColumns = mysql_query("SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='OCN' AND `TABLE_NAME`='cpu'") or die(mysql_error());
                $result = mysql_query('SELECT * FROM cpu') or die(mysql_error());

                while($columns = mysql_fetch_assoc($getColumns)){
                    echo "<th>". $columns['COLUMN_NAME'] ."</th>";
                }
                echo "</tr>";

                while($data = mysql_fetch_assoc($result)){
                    echo "<tr>";

                    foreach($data as $test){
                        echo "<td>". $test ."</td>";
                    }
                    echo "</tr>";
                }

                ?>
            </tr>
        </table>
    </body>
</html>

答案 1 :(得分:0)

尝试

  $checkResult = 'SELECT '.$meta->name.' FROM cpu WHERE '.$meta->name.'>'''' and '.$meta->name .' Is not null limit 1';