0% found this document useful (0 votes)
52 views

High Level PHP

The document discusses how to display data from a MySQL database table using PHP. It describes collecting user-submitted form data using HTML and inserting it into a MySQL table using a PHP script. It then explains how to retrieve and output that table data by writing a PHP script that selects the data from the table, loops through the rows, assigns the cell values to variables, and displays the formatted data in an HTML table.

Uploaded by

looklo
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

High Level PHP

The document discusses how to display data from a MySQL database table using PHP. It describes collecting user-submitted form data using HTML and inserting it into a MySQL table using a PHP script. It then explains how to retrieve and output that table data by writing a PHP script that selects the data from the table, loops through the rows, assigns the cell values to variables, and displays the formatted data in an HTML table.

Uploaded by

looklo
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

How to Display MySQL Table Data Tutorial

Learn how to populate and display MySQL table data with


PHP
Very often you will need to use a MySQL table to store data inside it and then output that data by
using a PHP script. To display the table data it is best to use HTML, which upon filling in some
data on the page invokes a PHP script which will update the MySQL table.

To populate a new database table with data you will first need an HTML page which will collect
that data from the user. The following HTML code that and passes the information to a PHP
script:

1 <form action="insert.php" method="post">


2 Value1: <input type="text" name="field1-name" />
3 Value2: <input type="text" name="field2-name" />
4 Value3: <input type="text" name="field3-name" />
5 Value4: <input type="text" name="field4-name" />
6 Value5: <input type="text" name="field5-name" />
7 <input type="Submit" /></form>

The above HTML code will show the user 5 text fields, in which the user can input data and a
Submit button. Upon clicking the Submit button the data submitted by the user will be passed to
a script named insert.php.

That script can have a syntax similar to the following:

<?php
1
$username = "your_username";
2
$password = "your_password";
3
$database = "your_database";
4
$field1-name=$_POST['Value1'];
5
$field2-name=$_POST['Value2'];
6
$field3-name=$_POST['Value3'];
7
$field4-name=$_POST['Value4'];
8
$field5-name=$_POST['Value5'];
9
$mysqli = new mysqli("localhost", $username, $password, $database);
10
@mysql_select_db($database) or die( "Unable to select database");
11
$query = "INSERT INTO tablename VALUES('','$field1-name','$field2-name','$field3-
12
name','$field4-name','$field5-name')";
13
$mysqli->query($query);
14
$mysqli->close();
15
?>
After the user submits the information, the insert.php script will save it in the database table.
Then you may want to output that information, so that the user can see it on the page. The first
command you will need to use is the SELECT FROM MySQL statement that has the following
syntax:

1 SELECT * FROM tablename;

This is a basic MySQL query which will tell the script to select all the records from the
tablename table. After the query is executed, usually you would want the result from it stored
inside a variable. This can be done with the following PHP code:

1 $query2="SELECT * FROM tablename";


2 $result=$mysqli->query($query2);

The whole content of the table is now included in a PHP array with the name $result. Before you
can output this data you should change each piece into a separate variable. There are two stages.

The first one is counting the rows. 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:

1 $num=$mysqli->mysqli_num_rows($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.

The second stage is to set up the loop. It will take each row of the result and print the data stored
there. In the code below, $i is the number of times the loop runs. This way all the records are
displayed.

1 $i=0;
2 while ($i < $num) {
3 CODE
4 $i++;
5 }

This is a basic PHP loop and will execute CODE the correct number of times. Each time $i will
be incremented by one. This is useful, as $i will tell the script which line of the results should be
read. As the first line in MySQL output is 0, this will work correctly.

The final part of the output script is to assign each piece of data to its own variable:

1 $field1-name=mysql_result($result,$i,"field1-name");
2 $field2-name=mysql_result($result,$i,"field2-name");
3 $field3-name=mysql_result($result,$i,"field3-name");
4 $field4-name=mysql_result($result,$i,"field4-name");
5 $field5-name=mysql_result($result,$i,"field5-name");

You do not need to get the ID field because there is no use for it in the output page. You can now
write a full script to output the data. In this script the data is not formatted when it is printed:

1 <?php
2 $username="username";
3 $password="password";
4 $database="your_database";
5 $mysqli = new mysqli("localhost", $username, $password, $database);
6 @mysql_select_db($database) or die( "Unable to select database");
7 $query2="SELECT * FROM tablename";
8 $result=$mysqli->query($query2);
9 $num=$mysqli->mysqli_num_rows($result);
10 $mysqli->close();
11 echo "<b>
12 <center>Database Output</center>
13 </b>
14 <br>
15 <br>";
16 $i=0;
17 while ($i < $num) {
18 $field1-name=mysql_result($result,$i,"field1-name");
19 $field2-name=mysql_result($result,$i,"field2-name");
20 $field3-name=mysql_result($result,$i,"field3-name");
21 $field4-name=mysql_result($result,$i,"field4-name");
22 $field5-name=mysql_result($result,$i,"field5-name");
23 echo "<b>
24 $field1-name $field2-name2</b>
25 <br>
26 $field3-name<br>
27 $field4-name<br>
28 $field5-name<hr>
29 <br>";
30 $i++;
31 }
32 ?>

This outputs a list of all the values stored in the database. This will give you a very basic output
which is not useful for a live website. Instead, it would be better if you could format it into a
table and display the information in it. To apply formatting you need to use HTML to print the
result by including the variables in the correct spaces. The easiest way to do this is by closing the
PHP tag and entering HTML normally. When you reach a variable position, include it as follows:

1 <? echo $variablename; ?>


in the correct position in your code.

You can also use the PHP loop to repeat the appropriate code and include it as part of a larger
table. The final output is:

1 <html>
2 <body>
3 <?php
4 $username="username";
5 $password="password";
6 $database="your_database";
7 $mysqli = new mysqli("localhost", $username, $password, $database);
8 @mysql_select_db($database) or die( "Unable to select database");
9 $query2="SELECT * FROM tablename";
10 $result=$mysqli->query($query2);
11 $num=$mysqli->mysqli_num_rows($result);?>
12 <table border="0" cellspacing="2" cellpadding="2">
13 <tr>
14 <td>
15 <font face="Arial, Helvetica, sans-serif">Value1</font>
16 </td>
17 <td>
18 <font face="Arial, Helvetica, sans-serif">Value2</font>
19 </td>
20 <td>
21 <font face="Arial, Helvetica, sans-serif">Value3</font>
22 </td>
23 <td>
24 <font face="Arial, Helvetica, sans-serif">Value4</font>
25 </td>
26 <td>
27 <font face="Arial, Helvetica, sans-serif">Value5</font>
28 </td>
29 </tr>
30 <?php
31 $i=0;
32 while ($i < $num) {
33 $f1=mysql_result($result,$i,"field1");
34 $f2=mysql_result($result,$i,"field2");
35 $f3=mysql_result($result,$i,"field3");
36 $f4=mysql_result($result,$i,"field4");
37 $f5=mysql_result($result,$i,"field5");?>
38 <tr>
39 <td>
40 <font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
41 </td>
<td>
42
<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
43
</td>
44
<td>
45
<font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
46
</td>
47
<td>
48
<font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font>
49
</td>
50
<td>
51
<font face="Arial, Helvetica, sans-serif"><?php echo $f5; ?></font>
52
</td>
53
</tr>
54
<?php
55
$i++;
56
}?>
57
</body>
58
</html>

This code will print out table content and add an extra row for each record in the database,
formatting the data as it is printed.

You might also like