Posted by: tonyteaching | July 12, 2009

to Display Data in MySQL database using PHP

How to display our data which are stored in MySQL database onto a page?

I’ll show my script first, then explain the components that we will need:

I insert PHP script into HTML tags.

<?php
$username=”yourusernameindatabase”;
$password=”yourpassword”;
$database=”databasename”;
$tablename=”tablename”;

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$query=”SELECT * FROM $tablename“;
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>

<table border=”1″ cellspacing=”2″ cellpadding=”2″>
<tr>
<th><font face=”arial narrow”>Mobile No.</font></th>
<th><font face=”arial narrow”>Darwin</font></th>
<th><font face=”arial narrow”>Alice Springs</font></th>
<th><font face=”arial narrow”>Katherine</font></th>
<th><font face=”arial narrow”>Tennant Creek</font></th>
<th><font face=”arial narrow”>Nhulunbuy</font></th>
</tr>

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

$f1=mysql_result($result,$i,”mobilenumber”);
$f3=mysql_result($result,$i,”D”);
$f4=mysql_result($result,$i,”A”);
$f5=mysql_result($result,$i,”K”);
$f6=mysql_result($result,$i,”T”);
$f7=mysql_result($result,$i,”N”);
?>

<tr>
<td><font face=”arial narrow”><?php echo $f1; ?></font></td>
<td><font face=”arial narrow”><?php echo $f3; ?></font></td>
<td><font face=”arial narrow”><?php echo $f4; ?></font></td>
<td><font face=”arial narrow”><?php echo $f5; ?></font></td>
<td><font face=”arial narrow”><?php echo $f6; ?></font></td>
<td><font face=”arial narrow”><?php echo $f7; ?></font></td>
</tr>

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

———————————————-

Here is the components that we need:

1. Openning up connection to database:

$username=”yourusernameindatabase”;
$password=”yourpassword”;
$database=”databasename”;
$tablename=”tablename”;

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);

2. After we connect to database, then select all data from the table using sql command: SELECT FROM

$query=”SELECT * FROM $tablename“;

3. Then we will execute the query using MySQL command: mysql_query ……….then  assign a variable to store the result

$result=mysql_query($query);

4. Before you can show  this data onto the page, you should change each piece into a separate variable. There are two stages.

a. The first one is counting the rows of data. Before you can go through the data in your result variable, you should know the number of the database rows. You could, of course, just type this into your code but it is not a very good solution as the script code will have to be changed every time a new row is added. Instead you can use the command: mysql_numrows

$num=mysql_numrows($result);

The $num value will be the number of rows stored in $result. This will be used in a loop to get all the data and display it on the screen.

b. The second stage is to set up the loop.

We will assign a variable for the number of looping, we start the loop from 0 and compare the variable and the $num.

We will ask variables to take value of cells in each row of the result and print the data stored on the page.

As long as the number of looping < $num (while we increase the number by 1 (++)) the commands between {  } will be repeated.

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

$f1=mysql_result($result,$i,”mobilenumber”);
$f3=mysql_result($result,$i,”D”);
$f4=mysql_result($result,$i,”A”);
$f5=mysql_result($result,$i,”K”);
$f6=mysql_result($result,$i,”T”);
$f7=mysql_result($result,$i,”N”);
?>

<tr>
<td><font face=”arial narrow”><?php echo $f1; ?></font></td>
<td><font face=”arial narrow”><?php echo $f3; ?></font></td>
<td><font face=”arial narrow”><?php echo $f4; ?></font></td>
<td><font face=”arial narrow”><?php echo $f5; ?></font></td>
<td><font face=”arial narrow”><?php echo $f6; ?></font></td>
<td><font face=”arial narrow”><?php echo $f7; ?></font></td>
</tr>

<?php
$i++;
}
?>


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: