Suhail Invento Suhail Invento - 6 months ago 13
PHP Question

How to print the whole table in MySQL

Currently I have this code that shows all the table in MySQL database but when trying to print it's not printing the whole table but showing only one row please helpme anybody cause it's in while loop

<?php

/* Do not edit */
include('connect.php');
session_start();
if ($_SESSION['username']) {
} else {
header("location:index");
}
/* Do not edit */
?>
<title><?php echo $_SESSION['username']; ?>'s Dashboard</title>
<link rel="stylesheet" href="css/responsive-nav.css">
<link rel="stylesheet" href="css/dashboard.css">
<link rel="stylesheet" href="css/rer.css">
<script src="js/responsive-nav.js"></script>
<script language="javascript">
function printdiv(printpage)
{
var headstr = "<html><head><title></title></head><body>";
var footstr = "</body>";
var newstr = document.all.item(printpage).innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML = headstr+newstr+footstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
</script>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>
<body>
<div class="top">
<img src="img/bnr.png" />
</div>
<hr color="#15aeec" />
<nav class="nav-collapse">
<ul>
<li><a href="dashboard.php">Home</a></li>
<li><a href="new-result.php">New Result</a></li>
<li><a href="all-result.php">All Result</a></li>
<li><a href="access.php">Accessibility</a></li>
<li><a href="logout.php">Logout</a></li>
</ul>
</nav>
<script>
var nav = responsiveNav(".nav-collapse");
</script>
<body>
<div id="body">
<div align="center"><input type="button" class=" btn" onClick="printdiv('body');" value=" Print Table "></div>
<div id="table">
<table width="1900px" cellspacing="2px" cellpadding="2px">

<tr>
<th class="box u" scope="col"> student id</th>
<th class="name u" scope="col">student name</th>
<th class="subject u"scope="col">kannada</th>
<th class="subject u"scope="col">english</th>
<th class="subject u"scope="col">hindi</th>
<th class="subject u"scope="col">maths</th>
<th class="subject u"scope="col">science</th>
<th class="subject u"scope="col">social</th>
<th class="result-exam u" scope="col">result</th>
<th class="result-exam u" scope="col">exam</th>
<th class="result-exam u" scope="col">added on</th>
</tr>
</table>
</div>

<?php
$dbhost = 'localhost';
$dbuser = 'sarvajna_users';
$dbpass = '22041956p@';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

if(! $conn )
{
die('Could not connect: ' . mysql_error());
}

$sql = 'SELECT * FROM result';
mysql_select_db('sarvajna_users');
$retval = mysql_query( $sql, $conn );

if(! $retval )
{
die('Could not get data: ' . mysql_error());
}

while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
?>
<!-- Main Content -->





<table width="1900px" cellspacing="2px" cellpadding="2px">


<tr>
<th class="box" scope="col"> <?php echo " " .$row['student_id']; ?></th>
<th class="name" scope="col"><?php echo " " .$row['student_name']; ?></th>
<th class="subject"scope="col"><?php echo " " .$row['kannada']; ?></th>
<th class="subject"scope="col"><?php echo " " .$row['english']; ?></th>
<th class="subject"scope="col"><?php echo "" .$row['hindi']; ?></th>
<th class="subject"scope="col"><?php echo " " .$row['maths']; ?></th>
<th class="subject"scope="col"><?php echo " " .$row['science']; ?></th>
<th class="subject"scope="col"><?php echo " " .$row['social']; ?></th>
<th class="result-exam" scope="col"><?php echo " " .$row['result']; ?></th>
<th class="result-exam" scope="col"><?php echo " " .$row['test']; ?></th>
<th class="result-exam" scope="col"><?php echo " " .$row['date']; ?></th>
</tr>
</table>

</div>


<?php }
mysql_close($conn);
?>
</body>

Answer

You should move your table tag outside the while loop as Niranjan said.

       <table width="1900px"  cellspacing="2px" cellpadding="2px">
   <?php while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
    {
    ?>
    <!-- Main Content -->
        <tr>
            <th class="box" scope="col"> <?php echo " " .$row['student_id']; ?></th>
            <th class="name" scope="col"><?php echo "  " .$row['student_name']; ?></th>
            <th class="subject"scope="col"><?php echo " " .$row['kannada']; ?></th>
            <th class="subject"scope="col"><?php echo " " .$row['english']; ?></th>
            <th class="subject"scope="col"><?php echo "" .$row['hindi']; ?></th>
            <th class="subject"scope="col"><?php echo " " .$row['maths']; ?></th>
            <th class="subject"scope="col"><?php echo " " .$row['science']; ?></th>
            <th class="subject"scope="col"><?php echo " " .$row['social']; ?></th>
            <th class="result-exam" scope="col"><?php echo " " .$row['result']; ?></th>
            <th  class="result-exam" scope="col"><?php echo " " .$row['test']; ?></th>
            <th  class="result-exam" scope="col"><?php echo " " .$row['date']; ?></th>
        </tr>

    <?php }?>
    </table>
</div> 
Comments