加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP PDO,不包括具有空字段值的行

发布时间:2020-12-13 22:27:40 所属栏目:PHP教程 来源:网络整理
导读:所以我使用这个示例代码output6.php 我想要做的是,如果empire_name返回null,则跳过关联的行 任何想法或建议将不胜感激 谢谢! !DOCTYPE htmlhtmlbody?phpecho "table style='border: solid 1px black;'"; echo "tr thEmpire Name/th thWin?/th thBuilding 1/
所以我使用这个示例代码output6.php

我想要做的是,如果empire_name返回null,则跳过关联的行
任何想法或建议将不胜感激
谢谢!

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

我试着查看这个,但我不知道该知道要找什么

谢谢!

我目前的成绩null = blank rows,image link

我觉得这很接近……
PDO and IS NOT NULL Function
但我不确定如何在当前代码中实现(我尝试了一些变体)

一个不同的变化 – 相同的结果(output3.php)更接近期望的最终结果

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

解决方法

在打印行之前简单添加检查.跟随之类的事情.

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>';
    }
}

要么

您可以在查询中添加WHERE原因.喜欢以下

WHERE empire_name IS NOT NULL AND empire != ''

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读