Home >Backend Development >PHP Tutorial >A simple web example code analysis combining php and database (php beginners)
This is a basic tutorial. No weird code, just some basics. There are currently a large number of tutorials based on UNIX machines, this tutorial will focus on Windows-based platforms.
However, except for the installation part, which has more or less Windows-specific instructions, the other parts are the same for all platforms. By the way, for the installation part, please see the installation guide on this site. In this tutorial, we will build a small website step by step using the following features of PHP and MySQL:
1. View the database;
2. Edit the database records;
3. Modify the database records;
4. Delete the database records.
We will learn MySQL and PHP at the same time and feel them together. This article can be learned directly from here. If you don’t know how to install and configure Apache+PHP+Mysql, please check out the related articles on the web teaching network!
First run the web server (PHP extension has been added); run MySQL.
Create and manipulate a MySQL database:
First we need to create the database and tables to be used. The database is named "example", the table is named "tbl", and has the following fields: identification number, first name, last name and information. To complete the database creation and table definition work through the mysql terminal, just double-click or run c:mysqlbinmysql.exe.
If you want to see which tables have been defined in MySQL, you can use (note that mysql> is the terminal prompt):
Mysql> show databases;
This command may display the following information:
+--- -------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.01 sec)
To define a new database (example), type:
Mysql> create database example;
You will see an answer such as:
Query OK, 1 row affected ( 0.17 sec) Great, we now have a new database. Now we can create a new table in the library, but first we need to select the new database:
Mysql> use example;
The answer should be:
Database changed
Now we can create the table, with the following fields:
Index number - integer
User name - a string with a maximum length of 30
User Last name - a string with a maximum length of 50
Free information - a string with a maximum length of 100
Type the following command at the MySQL prompt to create the table:
MySQL> create table tbl (idx integer(3), UserName varchar( 30), LastName varchar(50), FreeText varchar(100));
The answer should be:
Query OK, 0 rows affected (0.01 sec)
Okay, let’s see what it looks like to view the table from the MySQL prompt, type the command:
MySQL> show columns from tbl;
We will get the following results:
+----------+---------------+------+----- +---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---- ----------+------+-----+---------+-------+
| idx | int(3) | YES | | NULL | |
| UserName | varchar(30) | YES | | NULL | |
| LastName | varchar(50) | YES | | NULL | |
| FreeText | varchar(100) | YES | | |
+----------+---------------+------+-----+------- --+-------+
4 rows in set (0.00 sec)
Here, we can see the contents of the table "tbl" just created.
Now let’s take a look at what’s in the table. Type the following command:
MySQL> select * from tbl;
This command is used to display all data in table "tbl". The output might be:
Empty set (0.07 sec) The reason why we get this result is because we haven’t inserted any data into the table yet. Let's insert some data into the table and type:
MySQL> insert into tbl values (1,'Rafi','Ton','Just a test');
Query OK, 1 row affected ( 0.04 sec)
As you can see above, the values we insert into the table are in the order in which we defined the table earlier, because the default order is used. We can set the order of data, the syntax is as follows:
MySQL> insert into tbl (idx,UserName,LastName,FreeText) values (1,'Rafi','Ton','Just a test');
MySQL> select * from tbl;
The result this time is:
+------+------ ----+----------+-------------+
| idx | UserName | LastName | FreeText |
+------+- ----------+----------+-------------+
| 1 | Rafi | Ton | Just a test |
+- -----+----------+----------+-------------+
1 row in set (0.00 sec )
Now we can see the structure of the table and the content of each cell.
Now we want to delete the data.To achieve this we should type:
MySQL> delete from tbl where idx=1 limit 1;
Okay, give me some explanations. We are telling MySQL to delete records from the "tbl" table, delete those records with an idx field value of 1, and limit deletion to only one record. If we don't limit the number of deleted records to 1, then all records with idx 1 will be deleted (in this example we only have one record, but nonetheless, I just wanted to make this more clear).
Unfortunately, we get an empty table again, so let's enter it again:
MySQL> insert into tbl values (1,'Rafi','Ton','Just a test');
Query OK, 1 row affected (0.04 sec)
Another thing you can do is to modify the content of the specified field and use the "update" command:
MySQL>update tbl set UserName='Berber' where UserName=' Rafi';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
This command will search for all records whose UserName is "Rafi" and change it to "Berber". Note that the set part and where part do not have to be the same. We can search one field but change another field. Moreover, we can perform searches with two or more criteria.
MySQL>update tbl set UserName='Rafi' where UserName='Berber' and LastName='Ton';
Query OK, 1 row affected (0.04 sec)
This query searches two fields and changes the value of UserName
Combining PHP and MySQL
In this section, we will build a custom PHP-based web site, using To control the MySQL table created earlier.
We will build the following site structure (assuming you already know some basic HTML knowledge):
1. index.php3 is used to view the table on the front end 2. add.php3 is used to insert data into the table
3. Modify.php3 Used to modify records in the table 4. del.php3 is used to delete records in the table
First, we want to check the database and take a look at the following script:
---------------- -------------------------------------------------- ---------------
Index.php
mysql_connect() or die ("Problem connecting to DataBase");
$query = "select * from tbl";
$result = mysql_db_query("example", $query);
if ( $result) {
echo "Found these entries in the database:
";
echo "
";
User Name
Last Name
Domain Name< ;/td>
Request Date
["idx"];
$user = $r["UserName"];
$last = $r["LastName"];
$text = $r["FreeText"];
echo "
& lt; td & gt; $ IDX & LT;/TD & GT;
& LT; TD & GT; $ User & LT;/TD & GT;
& LT; TD & GT; $ last & lt;
& lt; td & gt; $ Text & LT;/TD & GT;
& LT;/TR & GT; " ;
}
echo "
}
else
{
echo "No data.";
}
mysql_free_result($result);
include ('links.x');
?>