Mysql
 sql >> Base de données >  >> RDS >> Mysql

Comment extraire uniquement les colonnes qui ont des valeurs non nulles dans mysql et php ?

Essayez ceci :

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

Cependant, avec mysql, vous pourrez peut-être même le faire :

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

J'espère que cela vous aidera

MODIFIER

Je peux voir ce que vous voulez maintenant que vous avez modifié la question :) Malheureusement, je ne connais pas de moyen de savoir ce que vous voulez en utilisant SQL (quelqu'un peut le faire).

Vous sortez les en-têtes de colonne et donc ne pas sortir une colonne particulière les ferait apparaître dans les mauvaises colonnes, vous n'aurez donc rien à sortir là où l'heure est 0000-00-00

C'est comme ça que je le ferais en PHP. (et si j'ai encore raté votre point, je peux me tirer une balle :))

<?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>

MODIFIER

Les en-têtes de tableau ne sont pas générés si le champ contient 0000-00-00. Cela dépend du fait qu'un seul élément est généré à la fois.

<?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>