Nnaerich Doughan
Nnaerich Doughan

Reputation: 31

PHP Problems , Doesnt Echo data From Database

With the following code in PHP, I want to echo the data fetched from some MySQL tables:

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

However, it doesn't work as I expect.

This is the output I'm getting:

enter image description here

How can I echo the data from the database?

Upvotes: 0

Views: 71

Answers (2)

devpro
devpro

Reputation: 16117

You need to print HTML result inside the while loop as:

$tablez_col = "
<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>";

echo $tablez_col; // print columns

// your loop start
while( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ) )
{
echo "
<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> 
</table>"; 
} // loop end

Upvotes: 0

Professor Abronsius
Professor Abronsius

Reputation: 33813

The variable $tablez should be defined inside the loop as it gets content from the recordset. As it was declared after the loop most likely you would have gotten some sort of error

To avoid the repetition of the table header, define /echo outside the loop and only repeat the tablerows inside the loop.

Rather than using inline styles it would be far easier to maintain the code if you used a stylesheet, that way you could remove all of the styling from the various html elements and govern the look and layout with one exteernal file.

$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 );

Upvotes: 2

Related Questions