<select name="year">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select>
<input type="submit" name="sub" value="Search"/>
</div>
<?php
if(isset($_REQUEST['sub'])){
$year=$_REQUEST['year'];

$con=mysql_connect("localhost","root","")or die( "Unable to connect");
mysql_select_db("mydb",$con) or die( "Unable to select database");
$query="SELECT * FROM student WHERE EnrolledYear='$year'";
$result=mysql_query($query)or die(mysql_error());;

$num=mysql_numrows($result);

mysql_close();
?>
<table>
<tr>
<th>Student ID No.</font></th>
<th>Name</font></th>
<th>Enrolled Terms</font></th>
<th>Enrolled Years</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"StudentID");
$f2=mysql_result($result,$i,"FullName");
$f3=mysql_result($result,$i,"EnrolledTerm");
$f4=mysql_result($result,$i,"EnrolledYear");
?>

<tr>
<td><?php echo $f1; ?></font></td>
<td><?php echo $f2; ?></font></td>
<td><?php echo $f3; ?></font></td>
<td><?php echo $f4; ?></font></td>

</tr>

<?php
$i++;
}
}
?>

Recommended Answers

All 3 Replies

I have found the error logic.
don't close the connection of mysql database... remove mysql_close();

the reason to this is you close the connection for fetching the data before loop initiates. in the result of the loop will be nothing.

your $num value will be set to 0 when you close connection then after that it will be having while loop that results to $i=0 and $num=0 will be while (0<0) that results to nothing when you iterate the sentinnel with i+=1

hi masterjiraya,

Thank you for answering my question. I removed the mysql_close(); already but still ... it's not printing anything. I appreciate your help...

Please take a look at my code again...maybe i'm still missin' something..

here is my revised code:

<html>
<head>
<script type="text/css">
table,th{
border:0;
cellpadding:2;
cellspacing:2;
font-family:Vardana,Arial;
}
</head>
</script>
<body>
<div align="center">
<select name="year">
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select>
<input type="submit" name="sub" value="Search"/>
</div>
<?php
if(isset($_REQUEST['sub'])){
$year=$_REQUEST['year'];

$con=mysql_connect("localhost","root","")or die( "Unable to connect");
mysql_select_db("mydb",$con) or die( "Unable to select database");
$query="SELECT * FROM student WHERE EnrolledYear='$year'";
$result=mysql_query($query)or die(mysql_error());;

$num=mysql_numrows($result);


?>
<table>
<tr>
<th>Student ID No.</font></th>
<th>Name</font></th>
<th>Enrolled Terms</font></th>
<th>Enrolled Years</font></th>
</tr>

<?php
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"StudentID");
$f2=mysql_result($result,$i,"FullName");
$f3=mysql_result($result,$i,"EnrolledTerm");
$f4=mysql_result($result,$i,"EnrolledYear");
?>

<tr>
<td><?php echo $f1; ?></font></td>
<td><?php echo $f2; ?></font></td>
<td><?php echo $f3; ?></font></td>
<td><?php echo $f4; ?></font></td>

</tr>

<?php
$i++;
}
}
?>
</table>
</body>
</html>

I've never seen a variable set this way:

$f1=mysql_result($result,$i,"StudentID");
// since you want all of your results printed anyway use mysql_fetch_array() like this:
<?php
while ($row = mysql_fetch_array($result)) {			
	$f1 = $row['StudentID'];
	$f2 = $row['FullName'];
	$f3 = $row['EnrolledTerm'];
	$f4 = $row['EnrolledYear'];
?>
<tr>
<td><?php echo $f1; ?></font></td>
<td><?php echo $f2; ?></font></td>
<td><?php echo $f3; ?></font></td>
<td><?php echo $f4; ?></font></td>
</tr>
<?php
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.