Wednesday, September 17, 2014

How to know total no of rows in Database table in PHP?

First create a database and a table in it.
Here database name = first_db and table name = students
Add some data in the table

<html>
<head>
<title>No of Rows</title>
</head>
<body>

<?php
$dbserver="localhost";

$dbusername="root";

$dbpassword="";

$dbname="first_db";

$link=mysql_connect($dbserver,$dbusername,$dbpassword);
mysql_select_db($dbname,$link);

if($_REQUEST['btn'])
{
$sql="select * from students"; // Here students is a database table name that we created in the database
$result=mysql_query($sql,$link);
$row=mysql_num_rows($result);

echo "Total no of rows : " . $row ;

mysql_close($link);
}
?>

<form action="totalnorows.php" method="POST">
<table>
<tr>
<td>
<input type="submit" name="btn" value="Get No" />
</td>
</tr>
</table>
</form>

</body>
</html>

Output :)

Screen 1 :)



Screen 2 :)

Screen 3 :)

No comments:

Post a Comment