PHP and MySQL basic tutorial (4)

Source: Internet
Author: User
PHP and MySQL basic tutorial (4), read basic PHP and MySQL tutorial (4), MySQL SQL for MySQL, first, you must keep in mind that every line of its command is ended with a semicolon (;), ...... There is no such thing as absolute. The same is true here. As I mentioned earlier, when a MySQL line is inserted into the PHP code, "> <LINKh MySQL SQL

For MySQL, the first thing you must remember is that every line of its command is ended with a semicolon (;), ...... There is no such thing as absolute. The same is true here.

As I mentioned earlier, when a MySQL line is inserted into the 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 make PHP syntax analyzer confused, so it is better to omit it. In this case, although the semicolon is omitted, PHP will automatically add it when executing the MySQL command.

There is also a case where no extra points are required. When you want to display the vertical arrangement of fields, rather than horizontal arrangement as usual, you can use G to end a line of SQL statements, then you will not use a semicolon, for example:

SELECT * FROM PENPALS

WHERE USER_ID = 1G
TEXT, DATE, and SET data types
Prepared by: Yangmei compilation clicks in this article: 114

A field in the MySQL data table must define a data type. There are about 25 options, most of which are straightforward, so there is not much to worry about. But there are a few things to mention.

TEXT is not a data type, although it may be said in some books. It should actually be "long varchar" or "MEDIUMTEXT ".

The format of the DATE data type is YYYY-MM-DD, for example. 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.

Wildcard

There are two types of SQL configuration characters: "*" and "% ". In different cases. For example, if you want to view all the database content, 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.
Not null and null records
Prepared by: Yangmei compilation clicks in this article: 114

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 left blank. For such records, MySQL will execute something for it:

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 my 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 = 'Liu'

AND LAST_NAME = 'ruifeng'

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 = 'Liu'

AND LAST_NAME = 'ruifeng'

AND (city like '$ city' or city is null)

");

Note that the keyword "IS" must be used when searching for NULL, but 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. This is also a MySQL Bug, so in this case, be especially careful when using SELECT queries.

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.