sql >> Database teknologi >  >> RDS >> Mysql

Hvordan udtrækkes kun kolonner, der ikke har nul-værdier i mysql og php?

Prøv dette:

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

Selvom du med mysql måske endda kan gøre dette:

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

Håber dette hjælper

REDIGER

Jeg kan se, hvad du vil nu, hvor du har ændret spørgsmålet :) Desværre kender jeg ikke til en vej til, hvad du vil bruge SQL (nogen kan).

Du udskriver kolonneoverskrifterne, og hvis du ikke udskriver en bestemt kolonne, vil de blive vist i de forkerte kolonner, så du skal bare ikke udskrive noget, hvor tiden er 0000-00-00

Sådan ville jeg dog gøre det i PHP. (og hvis jeg har misset din pointe igen, kan jeg skyde mig selv :))

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

REDIGER

Tabeloverskrifterne udlæses ikke, hvis feltet indeholder 0000-00-00. Dette afhænger af, at kun ét element udlæses ad gangen.

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



  1. MySQL GROUP BY adfærd

  2. Nye data vedvarer ikke til Rails-array-kolonnen på Postgres

  3. MySQL tilføjer en IKKE NULL kolonne

  4. Er det muligt at opgradere MySQL i MAMP til MySQL 5.7?