来自mysql php的不寻常的表输出

时间:2017-06-11 07:30:17

标签: php mysql

为什么我在表格输出中获得这些额外的图层?我需要从表中删除那些额外的图层。我做错了什么?这到底发生了什么?更新了代码。

<?php
$con=mysqli_connect("localhost","root", "", "cop");

if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql= mysqli_query ($con, "SELECT FirstName, Practice FROM Members");

echo "
    <style>th {
    font: bold 11px 'Trebuchet MS', Verdana, Arial, Helvetica,
    sans-serif;
    color: #6D929B;
    border-right: 1px solid #C1DAD7;
    border-bottom: 1px solid #C1DAD7;
    border-top: 1px solid #C1DAD7;
    letter-spacing: 2px;
    text-transform: uppercase;
    text-align: left;
    padding: 6px 6px 6px 12px;
    background: #CAE8EA url(images/bg_header.jpg) no-repeat;
}

th.nobg {
    border-top: 0;
    border-left: 0;
    border-right: 1px solid #C1DAD7;
    background: none;
}</style>


<table border='1'>
<tr>
<th>FirstName</th>
<th>Practice</th>
</tr>";

while($row = mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['Practice'] . "</td>";
echo "</tr>";
}

echo "</table>";

Unusual Table output

2 个答案:

答案 0 :(得分:0)

如果您要执行此类操作,您将能够判断数据中是否存在空列值

while( $row = mysqli_fetch_array($sql) ){

    if( !empty( $row['FirstName'] ) && !empty( $row['Practice'] ) ){
        $firstname=$row['FirstName'];
        $practice=$row['Practice'];
    } else{
        $firstname=$practice='empty';
    }

    echo "
        <tr>
            <td>{$firstname}</td>
            <td>{$practice}</td>
        </tr>";

}

或者,只要知道包含值而不是空单元格

,就输出该行
while( $row = mysqli_fetch_array($sql) ){

    if( !empty( $row['FirstName'] ) && !empty( $row['Practice'] ) ){
        $firstname=$row['FirstName'];
        $practice=$row['Practice'];
        echo "
            <tr>
                <td>{$firstname}</td>
                <td>{$practice}</td>
            </tr>";     
    }
}

答案 1 :(得分:0)

我将你的代码粘贴在我的Notepad ++上,其中包含浏览器无法原谅的错误。事实上,您甚至可以从浏览器中获得1个月的缓刑。以下是我的代码版本。我忽略了这种风格。您可以在代码工作后添加样式。

<?php
$con=mysqli_connect("localhost","root", "", "cop");

if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql= mysqli_query($con, "SELECT FirstName, Practice FROM Members");

$result = '';

if(mysqli_num_rows($sql) > 0){

while($row = mysqli_fetch_array($sql)){ 

$FirstName = $row['FirstName'];
$Practice  = $row['Practice'];

$result .= "<tr><td>" . $FirstName . "</td><td>" . $Practice . "</td></tr>";

    }

}

if(isset($result)){

echo "<table border='1'>";

echo "<tr><th>FirstName</th><th>Practice</th></tr>";

echo $result;

echo "</table>";

}

?>