What should I pay attention to when operating MySQL in PHP? _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags mysql commands
What issues should I pay attention to when operating MySQL in PHP. 1. the semicolon exception is in MySQL. every line of command ends with a semicolon (;). However, when a line of MySQL command is inserted in PHP code, it is best to omit the semicolon. for example 1, the semicolon exception

In MySQL, each line of command ends with a semicolon (;). However, when a line of MySQL command is inserted in PHP code, it is best to omit the semicolon. for example:

Mysql_query ("insert into tablename (first_name, last_name) VALUES ('$ first_name', '$ last_name ')");

This is because PHP also ends with a semicolon as a line. the extra semicolon may sometimes lead to incorrect analysis by the PHP syntax analyzer, so it is better to omit it. In this case, although the semicolon is omitted, PHP automatically adds the semicolon when executing the MySQL command.

There is also a case where no extra points are required. When you want to display the desired fields in a vertical arrangement, instead of arranging them horizontally as usual, you can use G to end a line of SQL statements. then, you cannot use a semicolon, for example:

SELECT * from penpals where USER_ID = 1G

2. TEXT, DATE, and SET data types

TEXT is not a data type. it should be "long varchar" or "MEDIUMTEXT ".

The format of the DATE data type is YYYY-MM-DD, for example: 2001-10-01. You can easily use the date function to obtain the current system time in this format:

Date ("Y-m-d ")

In addition, subtraction can be performed between DATA types to obtain the time difference days:

$ Age = ($ current_date-$ birthdate );

SET is a useful data type. it is similar to ENUM, except that SET can save multiple values while ENUM can only save one value. In addition, the SET type can have up to 64 predefined values, while the ENUM type can process up to 65,535 predefined values. What if we need a set with more than 64 values? In this case, you need to define multiple sets to solve this problem together.

3. wildcard characters

There are two types of SQL Wildcards: "*" and "% ". In different cases. For example, if you want to view all the content of the database, you can query it like this:

SELECT * FROM dbname WHERE USER_ID LIKE '% ';

Here, both wildcards are used. They mean the same ?? They are used to match any string, but they are used in different contexts. "*" Is used to match the field name, while "%" is used to match the field value. The wildcard "%" must be used with the LIKE keyword.

Another wildcard is the underscore "_", which represents a different meaning from the above and is used to match any single character.

4. not null and NULL records

What if the user presses the submit button without entering anything? If you really need a value, you can use a client script or a server script to perform data verification. However, in the database, some fields are allowed to be empty and nothing is required. For such records, MySQL will perform the following steps:

The insert value is NULL, which is the default operation.

If you declare not null in the field definition (when this field is created or modified), MySQL will leave this field blank and leave nothing blank.

For an ENUM enumeration field, MySQL inserts the first value of the enumeration set into the field if it is not null. That is to say, MySQL regards the first value of the enumeration set as the default value of this enumeration type.

There are some differences between a NULL record and an empty record. The % wildcard can match a NULL record, but cannot match a NULL record. In some cases, this difference may cause unexpected consequences. In terms of experience, any field should be declared as not null. In this way, the following SELECT query statement can run properly:

If (! $ CITY) {$ CITY = "% ";}
$ Selectresult = mysql_query ("SELECT * FROM dbname
WHERE FIRST_NAME = 'Bill'
AND LAST_NAME = 'gates'
And city like '$ city'
");

In the first line, if the user does not specify a CITY value, then the wildcard % will be used to substitute the CITY variable, so that any CITY value will be taken into account during the search, it even includes records with empty CITY fields.

However, if there are some records and its CITY field value is NULL, then the problem arises. The preceding query cannot find these fields. A solution to the problem can be as follows:

If (! $ CITY) {$ CITY = "% ";}
$ Selectresult = mysql_query ("SELECT * FROM dbname
WHERE FIRST_NAME = 'Bill'
AND LAST_NAME = 'gates'
AND (city like '$ city' or city is null)
");

Note that when searching for NULL, the keyword "IS" must be used, and LIKE does not work normally.

In the end, if you already have some records in the database before you add or modify a new field, the value of the newly added field is in the original record, it may be NULL or NULL. in this case, be extremely careful when using SELECT queries.

In MySQL, every line of commands is ended with a semicolon (;). But when a line of MySQL commands is inserted in PHP code, it is best to omit the semicolon, example...

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.