PHP and MySQL dynamic website development tutorial (1) _ MySQL

Source: Internet
Author: User
PHP and MySQL dynamic website development tutorial for Beginners (1) tip: from entry to mastery-PHP and MySQL beginners


This is a basic tutorial. No weird code, just some basics. Now there are a lot of tutorials based on UNIX machines, which will be focused on Windows platforms. However, apart from the installation part, there are more or less instructions for Windows, and the other part is the same for all platforms. By the way, for the installation part, please refer to 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 database records;
3. modify database records;
4. delete database records.
  
We will learn about MySQL and PHP at the same time and feel them together. This article will be learned from here. if you do not install and configure Apache + PHP + Mysql, please refer to the relevant articles on web tutorial network!

Run the web server (PHP extension added) first and MySQL.
  
Create and manipulate a MySQL database:
  
First, we need to create the database and table to be used. The database name is "example" and the table name is "tbl". It has the following fields: identification number, name, last name, and information. To create a database and define a table on the mysql terminal, double-click or run c:/mysql/bin/mysql.exe.
If you want to see which tables have been defined in MySQL, you can use (note mysql> 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, for example:
Query OK, 1 row affected (0.17 sec) is very popular. now we have a new database. Now we can create a new table in the database, 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-string with a maximum length of 30
User surname-string with a maximum length of 50
Free Info-string with a maximum length of 100
At the MySQL prompt, type the following command to create a 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)
Well, let's take a look at what the table looks like from the MySQL prompt. type the command:
MySQL> show columns from tbl; <回车>
We will get the following results:

Reference content is as follows:
+ ---------- + -------------- + ------ + ----- + --------- + ------- +
| 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 content of the newly created table "tbl.
  
Now let's take a look at what is in the table. Type the following command:
MySQL> select * from tbl; <回车>
  
This command is used to display all data in the table "tbl. The output may be:
Empty set (0.07 sec) returns this result because we have not inserted any data into the table. 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 shown in the preceding figure, the values inserted into the table follow the sequence defined in the previous table, because the default sequence is used. We can set the data sequence. The syntax is as follows:
MySQL> insert into tbl (idx, UserName, LastName, FreeText) values (1, 'Rafi ', 'ton', 'just a test '); <回车>
  
Okay. now let's take a look at the table content:
  
MySQL> select * from tbl; <回车>
The result 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, enter:
  
MySQL> delete from tbl where idx = 1 limit 1; <回车> Query OK, 1 row affected (0.00 sec)
  
Okay. give some explanations. We are telling MySQL to delete records from the "tbl" table, delete those records whose idx field value is 1, and delete only one record. If we do not limit the number of deleted records to 1, all records with idx as 1 will be deleted (in this example, we only have one record, but even so, I just want to make it clearer ).
  
Unfortunately, we once again got an empty table, so let's try again:
  
Reference content is as follows:
MySQL> insert into tbl values (1, 'Rafi ', 'ton', 'just a test '); <回车>
Query OK, 1 row affected (0.04 sec)

Another thing that can be done is to modify the content of the specified field and use the "update" command:

Reference content is as follows:
MySQL> update tbl set UserName = 'berberber' where UserName = 'Rafi '; <回车>
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0


This command will search all records whose UserName is "Rafi" and change it to "Berber ". Note that the set part and the where part are not necessarily the same. We can search for a field but change the other field. In addition, we can perform two or more search conditions.
  
Reference content is as follows:
MySQL> update tbl set UserName = 'raf' where UserName = 'berberber' and LastName = 'ton '; <回车>
Query OK, 1 row affected (0.04 sec)

This query searches for two fields and changes the UserName value.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.