Friday, September 19, 2014

Display data in the table from database.

First create the database and name it fdb.
Then create table with 2 columns in the database and name it simp.
Insert data in the table.

Demo :)

Now php file :)

<?php
$dbserver="localhost";
$dbusername="root";
$dbpassword="";
$dbname="fdb";

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

function displaytable($tablename,$link)
{
$query = "SELECT city FROM $tablename";
$res = mysql_query($query,$link);
$ccount = mysql_num_fields($res);

echo "<table border=1>";
echo ("<tr align=center>");
echo "<td><b>City List</b></td>";
echo "</tr>";

while($row=mysql_fetch_array($res))
{
echo ("<tr>");
for($col=0;$col<$ccount;$col++)
{
echo"<td> $row[$col]</td>\n";
echo"</tr>\n";
}
}
echo "</table>\n";
}
?>
<html>
<head>
<title>Display Table Data</title>
</head>
<body>
<h4>Table Data</h4>
<hr/>
<table>
<tr>
<td>
<?php
displaytable("simp",$link);
?>
</td>
</tr>
</table>
</body>
</html>

Output :)

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

Table of 25

<?php
$n=1;
factorial($n);
function factorial($n){
if($n<=10)
{
$ans= $n * 25;
echo "25 * $n"." = "."$ans"."<br/>";
factorial($n+1);
}
}
?>


Output :)