PHP问题,不从数据库回收数据

时间:2016-02-13 10:03:48

标签: php mysql

使用PHP中的以下代码,我希望回显从某些MySQL表中获取的数据:

<html>

<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Search Results</title>
</head>

<body bgcolor="#EFEFEF">
<table border="0" align=center width="95%" id="table1" bgcolor="#FFFFFF">
    <tr>
        <td>
        <img border="0" src="index_files/text827854609.gif" width="260" height="67"></td>
    </tr>
    <tr>
        <td><hr color="#C0C0C0" size="4"></td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>
        <p align="left"><font face="Verdana" size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        Search Student Name For Results</font></td>
    </tr>
    <tr>
        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </td>
    </tr>
    <tr>
        <td>
        <form method="POST" name="Informationform" action="searchresults.php">
            <!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" B-Reverse-Chronology="FALSE" S-Builtin-Fields -->
            <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <b><font face="Verdana" size="2">Enter Name in Full</font></b> :&nbsp;
            <input type="text" name="fullname" size="50">
            <input type="submit" value="Search" name="senda"></p>
        </form>
        <?php
        if(isset($_POST['senda'])){
            include 'mysqlconn.php';
            $con = mysqli_connect($host, $dbuser, $pass, $db) or die('Cannot Connect');
            $name = mysql_escape_string($con,$_POST['fullname']);

            $sql = "SELECT * FROM scores WHERE fullname = '$name'";
            $result = mysqli_query($con,$sql)  or die("Error: ".mysqli_error($con));
            while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
            {
                echo $tablez;
            }mysqli_free_result($result);
        }
        ?>
        </td>
    </tr>
    <tr>
        <td>
    $tablez = "<table border=0 width=99% id=table2>
            <tr>
                <td width=299 align=center><b>
                <font face=Verdana size=2 color=#808080>Full Name</font></b></td>
                <td width=171 align=center><b>
                <font face=Verdana size=2 color=#808080>studentNo</font></b></td>
                <td width=276 align=center><b>
                <font face=Verdana size=2 color=#808080>SubjectName</font></b></td>
                <td width=106 align=center><b>
                <font face=Verdana size=2 color=#808080>GPA</font></b></td>
                <td width=176 align=center><b>
                <font face=Verdana size=2 color=#808080>CGPA</font></b></td>
                <td align=center><b>
                <font face=Verdana size=2 color=#808080>SCORE</font></b></td>
            </tr>
            <tr>
                <td width=299 align=center> . $row['Fullname'] . </td>
                <td width=171 align=center>. $row['studentNo']. </td>
                <td width=276 align=center>. $row['SubjectName'].</td>
                <td width=106 align=center> .$row['GPA']. </td>
                <td width=176 align=center>.$row['CGPA']. </td>
                <td align=center> .$row['SCORE'].</td>
            </tr>
        </br></table>";
        </td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td><hr color="#808080" size="1">
        <p>
        <font face="arial" class="ws7" style="font-size: 9.3px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(212, 212, 212);">
        EM Software Limited Nigeria<span class="Apple-converted-space">&nbsp;</span></font><font class="ws7" style="font-size: 9.3px; color: rgb(0, 0, 0); font-family: arial; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(212, 212, 212)">©</font><font face="arial" class="ws7" style="font-size: 9.3px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(212, 212, 212);"><span class="Apple-converted-space">&nbsp;</span>2016 
        All rights Reserved.</font></td>
    </tr>
</table>
</body>

</html>

然而,它并没有像我期望的那样发挥作用。

这是我得到的输出:

enter image description here

如何回显数据库中的数据?

2 个答案:

答案 0 :(得分:2)

变量$tablez应该在循环内部定义,因为它从记录集中获取内容。因为在循环之后声明它很可能会出现某种错误

为了避免重复表头,在循环外定义/ echo,只重复循环内的tablerows。

如果您使用样式表,那么维护代码会更容易,而不是使用内联样式,这样您就可以从各种html元素中删除所有样式,并使用一个外部文件控制外观和布局。

$tableheader="
    <table border='0' width='99%' id='table2'>
        <tr>
                <td width='299px' align='center'><b>
                <font face='Verdana' size='2' color='#808080'>Full Name</font></b></td>
                <td width='171px' align='center'><b>
                <font face='Verdana' size='2' color='#808080'>studentNo</font></b></td>
                <td width='276px' align='center'><b>
                <font face='Verdana' size='2 'color='#808080'>SubjectName</font></b></td>
                <td width='106px' align='center'><b>
                <font face='Verdana' size='2' color='#808080'>GPA</font></b></td>
                <td width='176px' align='center'><b>
                <font face='Verdana' size='2' color='#808080'>CGPA</font></b></td>
                <td align='center'><b>
                <font face='Verdana' size='2' color='#808080'>SCORE</font></b></td>
        </tr>";
$tablefooter='</table>';


echo $tableheader;

while( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ) ){

    $tablerows = "
            <tr>
                <td width='299px' align='center'>" . $row['Fullname'] . "</td>
                <td width='171px' align='center'>". $row['studentNo']. "</td>
                <td width='276px' align='center'>". $row['SubjectName']."</td>
                <td width='106px' align='center'>" .$row['GPA']. "</td>
                <td width='176px' align='center'>".$row['CGPA']. "</td>
                <td align='center'>" .$row['SCORE']."</td>
            </tr>";
    /* echo the table row */
    echo $tablerows;
}

echo $tablefooter;/* close the table */
mysqli_free_result( $result );

答案 1 :(得分:0)

您需要在while循环中打印HTML结果:

def remove_batch_1
  @job_table.shift(BATCH_SIZE)
end