Learn how to display MySQL table data using PHP - NESTED CODE || TECH FLOAT

Breaking

Post Top Ad

Post Top Ad

Thursday, 1 November 2012

Learn how to display MySQL table data using PHP

Learn how to display MySQL table data

 In this tutorial blog we will learn how to display MySQl table data. In my MySQL Database, there has a table named "users". It is like 



Now we will show this in my php page.
code is written below just paste by opening a new notepad document and save it with extension ".php"

CODE::  

<html>
<body>
<?php
$mysql_host = 'localhost';
$mysql_user = 'root';
$mysql_pass = '';

$mysql_db = 'a_database';
if( !mysql_connect($mysql_host,$mysql_user,$mysql_pass) || !mysql_select_db($mysql_db))
{

    die(mysql_error());

}


$query="SELECT * FROM users";//'users' is my table name
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<td><font face="Arial, Helvetica, sans-serif">User Id</font></td>
<td><font face="Arial, Helvetica, sans-serif">Password(Encripted)</font></td>
<td><font face="Arial, Helvetica, sans-serif">First Name</font></td>
<td><font face="Arial, Helvetica, sans-serif">Last Name</font></td>
<!--<td><font face="Arial, Helvetica, sans-serif">Value5</font></td> -->
</tr>

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

$f1=mysql_result($result,$i,"username");
$f2=mysql_result($result,$i,"password");
$f3=mysql_result($result,$i,"firstname");
$f4=mysql_result($result,$i,"surname");

?>

<tr>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font></td>

</tr>

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

Now the output will be like::



No comments:

Post a Comment

Post Bottom Ad