PHP Beginner's Introduction to Database Table Operations
php Add
Used to add new records to the database table
Syntax:
INSERT INTO table_name VALUES ( value1, value2,....);
Note: table_name table name values(value)
Next let’s write an example to analyze
Note: First connect to the database, and then determine whether the connection is successful.
Write the added sql statement $username $password as a variable, which is the value you want to add to the database
Then execute the sql statement , determine whether the addition is successful! Finally, we need to enter the database table to see if the data has been added
Delete
DELETE FROM statement is used to delete records from the database table
Syntax: delete from table name where conditions
The code is as follows:
Note: Deletion requires conditions. There is a lot of information in the database table. Which one should be deleted?
So we usually get the id when deleting, and then delete the data based on the id, because the id is unique and the user’s name may be the same
Modify
##The UPDATE statement is used to modify data in the database table Syntax: UPDATE table_name SET column_name = new_value WHERE column_name = some_value
Example:
Query
Query statement select The statement is used to select data from the database Syntax: SELECT column_name(s) FROM table_name SQL statements are case-insensitive sensitive. SELECT is equivalent to select. In order for PHP to execute the above statement, we must use the mysql_query() function When we talked about functions in the previous section, we actually used the query statement Next Look at a few cases: Example:
"; } ?> Note: Query all the items in the table and output them数据表操作 查询 "; print_r($row); echo "
Query based on conditions
Format: select * from user where (condition);
Example:
"; } ?> Note: This will query and output the data with id 2 in our data table数据表操作 条件查询 "; print_r($row); echo "
Get 2 pieces of information from the database
"; } ?>数据表操作 查询 "; print_r($row); echo "
Attention
Everyone may be confused about limit1 and 2
This 1 represents which item to start from, 2 How many
are taken to sort:
When querying, the data must be displayed. For example, the id ranges from 1 to 1000, so there are 1000 When a piece of data is displayed on the page, the content must be updated as the id is larger, so at this time we have to use sorting
The default is ascending order, reverse order order by id desc
Ascending order asc
This sentence is to perform reverse order based on id
"; } ?>数据表操作 查询 "; print_r($row); echo "
Note: Please copy the above code locally for testing