Post in FormMethods and getWhat are the differences between methods?
A:
1. Get is to get data from the server, and post is to send data to the server.
2. Get is to add the parameter data queue to the URL referred to by the Action attribute of the submission form. The values correspond to each field in the form one by one and can be seen in the URL;
Post uses the http post mechanism to place fields in the form and their content in the HTML header and send them to the URL address referred to by the Action attribute. You cannot see this process.
3. For the get method, the server uses request. querystring to obtain the value of the variable. For the POST method, the server uses request. Form to obtain the submitted data.
4. The data volume transmitted by get is small and cannot exceed 2 kb. The amount of data transmitted by post is large, but it is limited by the configuration file.
5. Low get security and high post security.
Optimize MySQLDatabase methods?
A:
1. select the most suitable field attribute
MySQL can support access to large data volumes, but generally, the smaller the table in the database, the faster the query will be executed on it. Therefore, when creating a table, we can set the field width in the table as small as possible to achieve better performance. For example, if you set it to Char (255) when defining the zip code field, it is obvious that unnecessary space is added to the database, and even the varchar type is redundant, because char (6) can well complete the task. Similarly, if possible, we should use mediumint instead of bigin to define integer fields.
Another way to improve efficiency is to set the field to not null whenever possible, so that the database does not need to compare null values during future queries.
Some text fields, such as "Province" or "gender", can be defined as enum. In MySQL, The enum type is processed as the numeric data, and the numeric data is processed much faster than the text type. In this way, we can improve the database performance.
2. Use join instead of sub-queries)
MySQL supports SQL subqueries from 4.1. This technique can use the SELECT statement to create a single column query result, and then use this result as a filter condition in another query. For example, if you want to delete a customer who has no orders in the basic customer information table, you can use the subquery to retrieve the customer IDs of all orders from the sales information table, then pass the result to the primary query, as shown below:
DELETE FROM customerinfoWHERE CustomerID NOT in (SELECT CustomerID FROM salesinfo )
Subqueries can be used to complete SQL operations that require multiple logical steps at a time. At the same time, transactions or tables can be prevented from being locked and can be easily written. However, in some cases, subqueries can be replaced by more efficient join. For example, if we want to retrieve all users without order records, we can use the following query:
SELECT * FROM customerinfoWHERE CustomerID NOT in (SELECT CustomerID FROM salesinfo )
If you use join... to complete this query, the speed will be much faster. Especially when the salesinfo table has an index on customerid, the performance will be better. The query is as follows:
SELECT * FROM customerinfoLEFT JOIN salesinfoON customerinfo.CustomerID=salesinfo.CustomerIDWHERE salesinfo.CustomerID IS NULL
Join... it is more efficient because MySQL does not need to create a temporary table in the memory to perform the query in two steps.
3. Use Union instead of creating a temporary table manually
MySQL 4.0 and later versions support union queries. It can merge two or more select queries in a temporary table. When the query Session on the client ends, the temporary table is automatically deleted to ensure the database is neat and efficient. When using Union to create a query, we only need to use Union as the keyword to connect multiple select statements. Note that the number of fields in all select statements must be the same. The following example demonstrates a query using union.
SELECT Name, Phone FROM clientUNIONSELECT Name, BirthDate FROM authorUNIONSELECT Name, Supplier FROM product
4. Transactions
Although we can use sub-queries, join, and Union to create various queries, however, not all database operations can be completed with only one or a few SQL statements. In more cases, a series of statements are needed to complete some work. However, in this case, when a statement in the statement block fails to run, the operation of the entire statement block becomes uncertain. Imagine that you want to insert data into two associated tables at the same time. This may happen: after the first table is successfully updated, the database suddenly experiences an unexpected situation, as a result, the operations in the second table are incomplete, which may damage the data in the database. To avoid this situation, you should use a transaction. Its function is to either operate successfully or fail each statement in the statement block. In other words, data consistency and integrity can be maintained in the database. The transaction starts with the begin keyword and ends with the commit keyword. If an SQL operation fails, the rollback command can restore the database to the State before the start of in.
BEGIN;INSERT INTO salesinfo SET CustomerID=14;UPDATE inventory SET Quantity=11WHERE item='book';COMMIT;
Another important role of transactions is that when multiple users use the same data source at the same time, they can use the database locking method to provide users with a safe access method, this ensures that user operations are not affected by other users.
5. Lock the table
Although transactions are a very good way to maintain database integrity, they sometimes affect database performance, especially in large application systems. Because the database will be locked during transaction execution, other user requests can only wait until the transaction ends. If a database system is used by only a few users, the impact of transactions will not become a big problem. However, assuming that thousands of users access a database system at the same time, for example, accessing an e-commerce website may cause serious response latency.
In fact, in some cases, we can lock the table to achieve better performance. The following example uses the table locking method to complete the transaction function in the previous example.
LOCK TABLE inventory WRITESELECT Quantity FROM inventoryWHER EItem='book';...UPDATE inventory SET Quantity=11WHER EItem='book';UNLOCK TABLES
Here, we use a SELECT statement to retrieve the initial data. Through some calculations, we use the update statement to update the new value to the table. The lock TABLE statement containing the write keyword ensures that no other access is allowed to insert, update, or delete the inventory before the unlock tables command is executed.
6. Use foreign keys
Locking a table can maintain data integrity, but it cannot guarantee data relevance. In this case, we can use the foreign key. For example, a foreign key can ensure that each sales record points to an existing customer. Here, the foreign key can map the customerid in the customerinfo table to the customerid in the salesinfo table. No record without a valid customerid will be updated or inserted into the salesinfo table.
CREATE TABLE customerinfo(CustomerID INT NOT NULL ,PRIMARY KEY ( CustomerID )) TYPE = INNODB;CREATE TABLE salesinfo(SalesID INT NOT NULL,CustomerID INT NOT NULL,PRIMARY KEY(CustomerID, SalesID),FOREIGN KEY (CustomerID) REFERENCES customerinfo(CustomerID) ON DELETECASCADE) TYPE = INNODB;
Note the parameter "on Delete cascade" in the example ". This parameter ensures that when a customer record in the customerinfo table is deleted, all records related to this customer in the salesinfo table will also be deleted automatically. If you want to use foreign keys in MySQL, remember to define the table type as the InnoDB type of the Transaction Security table when creating the table. This type is not the default MySQL table type. The defined method is to add type = InnoDB In the create table statement. As shown in the example.
7. Use Indexes
Index is a common method to improve database performance. It allows database servers to retrieve specific rows at a much faster speed than no index, especially when a query statement contains max (), when using the min () and orderby commands, the performance improvement is more obvious. Which fields should be indexed? In general, the index should be built on the fields that will be used for join, where judgment and order by sorting. Try not to index a field in the database that contains a large number of repeated values. For an Enum type field, it is very likely that a large number of repeated values appear, such as "Province" in customerinfo ".. it is not helpful to create an index on such a field. On the contrary, it may also reduce the performance of the database. When creating a table, we can create an appropriate index at the same time, or use alter table or create index to create an index later. MySQL supports full-text indexing and search from version 3.23.23. Full-text index is a Fulltext index in MySQL, but it can only be used for tables of the MyISAM type. For a large database, it is very fast to load data to a table without Fulltext indexes and then create an index using alter table or create index. However, if you load data to a table with a Fulltext index, the execution will be very slow.
8. Optimized query statements
In most cases, using indexes can increase the query speed. However, if an SQL statement is improperly used, indexes cannot play its due role. The following are some notes. First, it is best to compare fields of the same type. Before MySQL 3.23, this is even a required condition. For example, you cannot compare an indexed int field with a bigint field. However, in special cases, when a char field is of the same size as a varchar field, you can compare them. Second, do not use functions to operate indexed fields.
For example, when the yeae () function is used on a date field, the index cannot play its due role. Therefore, although the two queries below return the same results, the latter is much faster than the former.
SELECT * FROM order WHERE YEAR(OrderDate)<2001;SELECT * FROM order WHERE OrderDate<"2001-01-01";
In the same case, when the numeric field is calculated:
SELECT * FROM inventory WHERE Amount/7<24;SELECT * FROM inventory WHERE Amount<24*7;
The above two queries also return the same results, but the subsequent query will be much faster than the previous one. Third, when searching for a struct field, we sometimes use the like keyword and wildcard. Although this method is simple, it is at the cost of system performance. For example, the following query will compare each record in the table.
SELECT * FROM booksWHERE name like "MySQL%"
However, if the following query is used, the returned results are the same, but the speed is much faster:
SELECT * FROM booksWHERE name>="MySQL"and name<"MySQM"
Finally, you should note that you should avoid making MySQL automatically convert the data type during the query, because the conversion process also makes the index ineffective.
Query speed improvement through Query Buffer
When SQL statements are used for query, the database server executes this SQL statement every time it receives the SQL statement sent from the client. However, when the same SQL statement is received within a certain interval (for example, within 1 minute), it is also executed. Although this can ensure the real-time data, in most cases, the data does not require full real-time, that is, there can be a certain latency. If this is the case, executing the same SQL statement in a short time is not worth the candle.
Fortunately, MySQL provides the query buffer function (query buffering can only be used in MySQL 4.0.1 and later versions ). We can improve query performance to a certain extent through query buffering.
You can set the Query Buffer through the my. ini file in the MySQL installation directory. You can set query_cache_type to 1. After this attribute is set, MySQL will query whether the same SELECT statement has been executed in its buffer before executing any SELECT statement. If yes, if the execution result does not expire, the query result is directly returned to the client. However, when writing SQL statements, note that the Query Buffer of MySQL is case sensitive. For example, the following two select statements:
Code:
SELECT * from TABLE1SELECT * FROM TABLE1
The preceding two SQL statements have different select statements for query buffering. In addition, query buffering does not automatically process spaces. Therefore, when writing SQL statements, try to reduce space usage, especially at the beginning and end of the SQL statement (because, query buffering does not automatically intercept spaces at the beginning and end ).
Although no Query Buffer is set, it may cause performance loss, but some SQL statements need to query data in real time or are not frequently used (it may be executed once or twice a day ). In this way, you need to turn off the buffer. Of course, this can be done by setting the query_cache_type value to disable the Query Buffer, but this will permanently disable the query buffer. MySQL 5.0 provides a method to temporarily disable query buffering:
Code:
Select SQL _no_cache field1, field2 from Table1
The preceding SQL statement uses SQL _no_cache. Therefore, no matter whether the SQL statement has been executed or not, the server will not find it in the buffer zone and execute it every time.
We can also set query_cache_type in my. ini to 2, so that the query buffer is used only after SQL _cache is used.
Code:
SELECT SQL_CALHE * FROM TABLE1
How can I intercept Chinese strings without garbled characters?
Mb_substr ()
SessionAnd Cookie?
1. The session is saved on the server, and the client does not know the information. The cookie is saved on the client, and the server can know the information.
2. sessions cannot be differentiated. During a user's access to a website, all sessions can be accessed in any place. If path parameters are set in the cookie, the cookies in different paths of the same website cannot access each other.
3. By default, sessions can work normally only with cookies. If the client completely disables cookie and session, this method will become invalid.
4. The session will be closed after the user session ends. However, because the cookie is saved on the client, it can be saved for a long time.
5. From the application point of view, a session is an action state that persists on the server until it is not used. In subsequent requests, the client will return this information to the server to determine its identity. It is useful for the browser to maintain information during repeated accesses.
Do you need to replace the existing two variable values without new variables?
<?php$a =1;$b =2;list($a,$b) = array($b,$a);echo $a,$b;