简体   繁体   中英

PHP PDO, excluding row with null field value

So I am using this sample code output6.php

What I would like to do is if empire_name returns null, skip the associated row Any ideas or suggestions would be appreciated Thanks!

<!DOCTYPE html>
<html>
<body>

<?php
echo "<table style='border: solid 1px black;'>";
  echo "<tr>
    <th>Empire Name</th>
    <th>Win?</th>
    <th>Building 1</th>
    <th>Building 2</th>
    <th>Building 3</th>
    <th>Building 4</th>
    <th>Building 5</th>
    <th>Building 6</th>
    <th>Building 7</th>
    <th>Building 8</th>
    <th>Building 9</th>
    </tr>";

class TableRows extends RecursiveIteratorIterator { 
     function __construct($it) { 
         parent::__construct($it, self::LEAVES_ONLY); 
     }

     function current() {
         return "<td style='width: 150px; border: 1px solid black;'>" . parent::current(). "</td>";
     }

     function beginChildren() { 
         echo "<tr>"; 
     } 

     function endChildren() { 
         echo "</tr>" . "\n";
     } 
} 

$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";

try {
     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $stmt = $conn->prepare("SELECT empire_name, win, building_1, building_2, building_3, building_4, building_5, building_6, building_7, building_8, building_9 FROM ft_form_2 "); 
     $stmt->execute();

     // set the resulting array to associative
     $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 

     foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { 
         echo $v;
     }
}
catch(PDOException $e) {
     echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>  

</body>
</html>

I tried looking this up but I don't know enough to know what to look for

thanks!

my current results null = blank rows, image link

I think this is close... PDO and IS NOT NULL Function But I am not sure how to implement in the current code (I tried a few variations)

A different variation - same results (output3.php) closer to the desired end result

<?php
define("DB_HOST", "xxx");    // Using Constants
define("DB_USER", "xxx");
define("DB_PASS", "xxx");
define("DB_NAME", "xxx");

try {       // << using Try/Catch() to catch errors!

$dbc = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset-utf8",DB_USER,DB_PASS);
}catch(PDOException $e){ echo $e->getMessage();}

if($dbc <> true){
    die("<p>There was an error</p>");
}

$print = ""; // assign an empty string 

$stmt = $dbc->query("SELECT * FROM ft_form_2"); // fetch data
$stmt->setFetchMode(PDO::FETCH_OBJ);

if($stmt->execute() <> 0)
{

    $print .= '<table border="1px">';
    $print .= '<tr><th>Empire_Name</th>';
    $print .= '<th>Win</th>';
    $print .= '<th>Building_1</th>';
    $print .= '<th>Building_2</th>';
    $print .= '<th>Building_3</th>';
    $print .= '<th>Building_4</th>';
    $print .= '<th>Building_5</th>';
    $print .= '<th>Building_6</th>';
    $print .= '<th>Building_7</th>';
    $print .= '<th>Building_8</th>';
    $print .= '<th>Building_9</th> </tr>';

    while($ft_form_2 = $stmt->fetch()) // loop and display data
    {

        $print .= '<tr>';
        $print .= "<td>{$ft_form_2->empire_name}</td>";
        $print .= "<td>{$ft_form_2->win}</td>";
        $print .= "<td>{$ft_form_2->building_1} <br> {$ft_form_2->building_1_notes} </td>";
        $print .= "<td>{$ft_form_2->building_2} <br> {$ft_form_2->building_2_notes} </td>";
        $print .= "<td>{$ft_form_2->building_3} <br> {$ft_form_2->building_3_notes} </td>";
        $print .= "<td>{$ft_form_2->building_4} <br> {$ft_form_2->building_4_notes} </td>";
        $print .= "<td>{$ft_form_2->building_5} <br> {$ft_form_2->building_5_notes} </td>";
        $print .= "<td>{$ft_form_2->building_6} <br> {$ft_form_2->building_6_notes} </td>";
        $print .= "<td>{$ft_form_2->building_7} <br> {$ft_form_2->building_7_notes} </td>";
        $print .= "<td>{$ft_form_2->building_8} <br> {$ft_form_2->building_8_notes} </td>";
        $print .= "<td>{$ft_form_2->building_9} <br> {$ft_form_2->building_9_notes} </td>";
        $print .= '</tr>';
    }

    $print .= "</table>";
    echo $print;
}
?>  

To follow your logic of using IS NOT NULL you mentioned in your question. You were on the right path. Returning just what you are looking for from the database is way more efficient then getting everything and filtering it in PHP.

Your statement here

$stmt = $conn->prepare("SELECT empire_name, win, building_1, building_2, building_3, building_4, building_5, building_6, building_7, building_8, building_9 FROM ft_form_2 ");

you can add a where condition at the end

$stmt = $conn->prepare("SELECT empire_name, win, building_1, building_2, building_3, building_4, building_5, building_6, building_7, building_8, building_9 FROM ft_form_2 WHERE empire_name IS NOT NULL ");

Then your database will be doing the filtering. The same can be applied to your second example as well.

$stmt = $dbc->query("SELECT * FROM ft_form_2 WHERE empire_name IS NOT NULL");

Simple add check before printing rows. Something like following.

while($ft_form_2 = $stmt->fetch()) // loop and display data
{
    if( !empty($ft_form_2->empire_name) && !is_null($ft_form_2->empire_name) )
    {
        $print .= '<tr>';
        $print .= "<td>{$ft_form_2->empire_name}</td>";
        $print .= "<td>{$ft_form_2->win}</td>";
        $print .= "<td>{$ft_form_2->building_1} <br> {$ft_form_2->building_1_notes} </td>";
        $print .= "<td>{$ft_form_2->building_2} <br> {$ft_form_2->building_2_notes} </td>";
        $print .= "<td>{$ft_form_2->building_3} <br> {$ft_form_2->building_3_notes} </td>";
        $print .= "<td>{$ft_form_2->building_4} <br> {$ft_form_2->building_4_notes} </td>";
        $print .= "<td>{$ft_form_2->building_5} <br> {$ft_form_2->building_5_notes} </td>";
        $print .= "<td>{$ft_form_2->building_6} <br> {$ft_form_2->building_6_notes} </td>";
        $print .= "<td>{$ft_form_2->building_7} <br> {$ft_form_2->building_7_notes} </td>";
        $print .= "<td>{$ft_form_2->building_8} <br> {$ft_form_2->building_8_notes} </td>";
        $print .= "<td>{$ft_form_2->building_9} <br> {$ft_form_2->building_9_notes} </td>";
        $print .= '</tr>';
    }
}

Or

You can add WHERE cause in your query. Like following

 WHERE empire_name IS NOT NULL AND empire != ''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM