sql >> Base de Datos >  >> RDS >> Mysql

¿Cómo extraer solo columnas que tienen valores distintos de cero en mysql y php?

Prueba esto:

$result = mysqli_query($conn, "SELECT * FROM $dbname.statusinfo WHERE soid = '$userinput1' AND date_column <> '0000-00-00' ") or die(mysqli_error($conn));

Aunque, con mysql, incluso puede hacer esto:

$result = mysqli_query($conn, "SELECT * FROM $dbname.statusinfo WHERE soid = '$userinput1' AND date_column > '0000-00-00' ") or die(mysqli_error($conn));

Espero que esto ayude

EDITAR

Puedo ver lo que quieres ahora que modificaste la pregunta :) Desafortunadamente, no conozco una forma de hacer lo que quieres usando SQL (alguien puede hacerlo).

Está generando los encabezados de las columnas y, por lo tanto, si no genera una columna en particular, aparecerán en las columnas incorrectas, por lo que no tendrá que generar nada cuando el tiempo sea 0000-00-00

Sin embargo, así es como lo haría en PHP. (y si no he entendido tu punto otra vez, puedo pegarme un tiro :))

<?php
    $userinput1 = $_POST['soid'];

    $servername = "localhost";
    $username   = "root";
    $password   = "";
    $dbname     = "status";

    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_errno) {
        printf("Connect failed: %s\n", $conn->connect_error);
        exit();
    }

    $result = mysqli_query($conn, "SELECT * FROM $dbname.statusinfo WHERE soid = '$userinput1'  ") or die(mysqli_error($conn));

    $arrayHeadings = array(
        "dept"                  => "Department", 
        "samplerecived"         => "Sample Recived",
        "molbioextraction"      => "Mol-Bio Extraction",
        "molbioextractionqc"    => "Extraction QC",
        "libraryprep"           => "Library Prep",
        "libraryqc"             => "Library QC",
        "sequencing"            => "Sequencing",
        "datacheck"             => "Data Check",
        "resequencing"          => "RE Sequencing",
        "qccheck"               => "QC Check",
        "analysisstarted"       => "Analysis Started",
        "analysiscompleted"     => "Analysis Completed",
        "report"                => "Report",
        "outbound"              => "Outbound",
    );

?>
<style>
    th{
        color: blue;
    }

    td{
        color: black;
    }
</style>

<table border='1'>
    <tr>
        <?php foreach($arrayHeadings as $key => $name): ?>
            <th><?= $name; ?></th>
        <?php endforeach; ?>
    </tr>
    <tr>
        <?php while($row = mysqli_fetch_assoc($result)): ?>
            <?php foreach($arrayHeadings as $key => $name): ?>
                <?php if($row[$key] != "0000-00-00"): ?>
                    <td><?= $row[$key]; ?></td>
                <?php else: ?>
                    <td></td>
                <?php endif; ?>
            <?php endforeach; ?>
        <?php endwhile; ?>
    </tr>
</table>

EDITAR

Los encabezados de la tabla no se muestran si el campo contiene 0000-00-00. Esto depende de que solo se envíe un elemento a la vez.

<?php
    $userinput1 = $_POST['soid'];

    $servername = "localhost";
    $username   = "root";
    $password   = "";
    $dbname     = "status";

    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_errno) {
        printf("Connect failed: %s\n", $conn->connect_error);
        exit();
    }

    $result = mysqli_query($conn, "SELECT * FROM $dbname.statusinfo WHERE soid = '$userinput1'  ") or die(mysqli_error($conn));

    $arrayHeadings = array(
        "dept"                  => "Department", 
        "samplerecived"         => "Sample Recived",
        "molbioextraction"      => "Mol-Bio Extraction",
        "molbioextractionqc"    => "Extraction QC",
        "libraryprep"           => "Library Prep",
        "libraryqc"             => "Library QC",
        "sequencing"            => "Sequencing",
        "datacheck"             => "Data Check",
        "resequencing"          => "RE Sequencing",
        "qccheck"               => "QC Check",
        "analysisstarted"       => "Analysis Started",
        "analysiscompleted"     => "Analysis Completed",
        "report"                => "Report",
        "outbound"              => "Outbound",
    );

?>
<style>
    th{
        color: blue;
    }

    td{
        color: black;
    }
</style>

<table border='1'>
    <tr>
        <?php foreach($arrayHeadings as $key => $name): ?>
            <?php if($row[$key] != "0000-00-00"): ?>
                <th><?= $name; ?></th>
            <?php endif; ?>
        <?php endforeach; ?>
    </tr>
    <tr>
        <?php while($row = mysqli_fetch_assoc($result)): ?>
            <?php foreach($arrayHeadings as $key => $name): ?>
                <?php if($row[$key] != "0000-00-00"): ?>
                    <td><?= $row[$key]; ?></td>
                <?php endif; ?>
            <?php endforeach; ?>
        <?php endwhile; ?>
    </tr>
</table>