PHP introductory tutorial-database operation_PHP tutorial
php tutorial introductory tutorial-database tutorial operation
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 database records;
3. Modify database records;
4. Delete the records in the database.
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 tutorial 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 a table with the following fields:
Index number - integer
Username - a string with a maximum length of 30
User last name - a string with a maximum length of 50
Free message - string with maximum length 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 | | NULL | |
+----------+---------------+------+-----+----------+ -------+
4 rows in set (0.00 sec)
Here, we can see the contents of the table "tbl" we 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:
The reason why Empty set (0.07 sec) gets this result is because we have not inserted any data into the table. Let's insert some data into the table by typing:
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’);
Okay, now we can take a look at the contents of the table again:
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 contents 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 type it in 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 using 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 with UserName "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 part, we will create a simple PHP-based web site to control the MySQL table created earlier.
We will create 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 is used to modify the records in the table 4. del.php3 is used to delete the records in the table
First, we want to check the database and take a look at the following script:
-------------------------------------------------- -------------------------------
Index.php
Data from tbl
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 | Request Date |
$idx | $user | $last | $text |
}
else
{
echo "No data.";
}
mysql_free_result($result);
include (’links.x’);
?>
--------------------------------------------------------------------------------
好,下面给出一些说明:
我们先用正常的html标签创建thml文档。当我们想从html中出来转入PHP中时,我们用来结束PHP部分。
mysql_connect() 命令告诉PHP建立一个与MySQL服务器的连接。如果连接建立成功,脚本将继续,如果不成功,则打印出die命令的信息“Problem connecting to Database”(如果要看关于mysql_connect的更多的信息和其它的PHP函数,可以去http://www.php.net下的文档中查找)。
现在,如果MySQL是按照我们上面所讨论的那样安装的,就足够了。但是如果你使用的是预装的MySQL(象ISP),你应该使用下面的命令:
mysql_connect (localhost, username, password);
我们可以将$query设成我们想在MySQL中执行的查询,然后使用mysql_db_query命令来执行它:
$result = mysql_db_query("example", $query);
这时,"example"表示数据库的名字并且$query是要进行的查询。
我们使用MySQL命令select(象上面所描述的)来从表中取得所有的数据:
$query = "select * from tbl";
简单地解释一下$result的作用,如果执行成功,函数将返回一个查询结果的一个MySQL结果标识符,如 果出错则返回false。返回的不是结果而是一个标识符,可以在后面将它转换成我们所需的信息。
现在,我们想检查一下在数据库中是否存在有记录,并且如果有则将结果按照html的表格结构打印出来。为了检查是否存在数据,我们使用if命令和下面的语法:
if (argument) {
"do something;"
} else {
"do something different;"
}
这时"do something"当argument=true时你所要执行的命令,"do something different"为当argument =false时所要执行的命令。
注意我们使用echo命令来输出一些html标签来建立html的表格结构。只有从PHP命令输出的文本才会被 看成html内容 - PHP命令本身是不会看成html内容的。我们使用的另一个命令是while指令,使用格式如下:
while (argument)) {
"something to do";
}
while循环在argument=true时会不停地重复,执行在{}中的指令集。
Here we combine the while loop and the PHP function $r=mysql_fetch_array($result). This function retrieves a record based on the corresponding result identifier and places the result in an associative array $r, using the field name as the array key. In our script we will get an array: $r[’idx’], $r[’UserName’], $r[’LastName’] and
$r[’FreeText’].
We can also use the mysql_fetch_row function, which will put the results in an ordered array. We can use $r[0], $r[1], $r[2] and $r[3] to get the corresponding value. .
Now that we have all the information, we can print it out in an html table:
The following is the quoted content:
echo "
Now we can release the MySQL connection and release some resources by using the mysql_free_result($result) function.
Another useful feature of PHP is the ability to include text files in scripts. Let us assume that you have some reusable code (such as links to other pages), we can use the include function, which can save some code and time. Moreover, if we want to change this code, we only need to change the content of the included file, and it will take effect in all files that include it.
Here we create a text file called Links.x, which will store all the link menus we want to use on each page.
The syntax of include is:
Include (’included_text_file’);
Now we can use ?> to close the PHP part, and use

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software