PHP Link Database

1. Introduction PHP built-in function
to introduce common functions of PHP as a reference, then the operation is very easy.

1) connect to the database

 Mysql_connect (ip, user name, password) or die ( 'connection failed!');

2) Select the database (two ways)

Method a: Mysql_query ( 'use database name');

Method two: Mysql_select_db ( 'database name');

3) Set the client's character encoding

If you experience problems garbled add the following string of code is ok.

sql_query (set names UTF-8); // character to solve the garbage problem

4) execute SQL statements

Mysql_query (SQL statements);

5) Fetch a row from the result of the query results returned as an array

mysql_fetch_row();

This function is mainly used for a query result set, except that the function return value is not a string, but an array. After removing the last line, the function returns false, the loop ends. Can all data taken out and the results show a progressive set.

Example:

? <PHP

$ sql = mysql_query ( "the SELECT * from table_name"); // will store all the data to the table table_name $ sql

$ Row = mysql_fetch_row ($ sql); // get the first $ sql inside as an array is stored in the $ row

if ($ _POST [ "Submit" ] == " query") {
// If the previous value is received at 'query'

    $ keyword $ _POST = [ "Red"]; // received from the front end to query keywords stored in the name $

    $ SQL = mysql_query ( "the SELECT * from table_name like the WHERE Color '.%" the TRIM ($ keyword) "%.'"); // execute fuzzy query

    $ row = mysql_fetch_row ($ sql ); // get row query result, the return value is an array

}

>?

2. Connect database
generally in the company which is the second development framework, not in code to write their own connection to the database. To put it plainly, it is nothing more than a function of the database connection is used.

Example:

method one:

? <PHP

$ Link = @ mysql_connect ( 'connection failed!') ( 'URL', ' database account', 'database password') or Die;

mysql_select_db ( 'database name') or die ( 'selection fails!');

?>
way:

? <PHP
$ myhost = 'localhost: 3306'; // MySQL server host address
$ myusername = 'root'; // mysql username
$ mypassword = 'DJun'; // mysql username and password
$ conn = mysqli_connect ($ myhost , MyUserName $, $ mypassword);
IF (! $ connect)
{
Die ( 'connection failed:' mysqli_error ($ connect.));
}

the mysqli_query ($ connect, "sET UTF8 names"); // set the encoding prevent Chinese distortion
3. To achieve CRUD
CRUD may be implemented in three ways: a command interface (the CMD), the page code, software.

At the command of the interface and the page code, corresponding to four types of CRUD SQL statements, the principle is the same. Another way is to use software such as graphical user Navicat similar. Simple to introduce four types of SQL statements:

1) add data 
INSERT statement in the form of standard SQL statement:

INSERT INTO tablename (table name) VALUES (column value);
Example:

INSERT INTO users (id, name, age) VALUES (1102, 'DJun', 21);
and in another form there MySQL:

INSERT INTO tablename SET column_name1 = value1, column_name2 = value2,…;
实例:

The INTO the SET ID = Users the INSERT 1102, name = 'Djun', Age = 21 is;
2) delete the data
general syntax:

(Supplement: [WHERE Clause] represents the screening data statement)

DELETE FROM table_name [WHERE Clause]
实例:

The FROM Users the WHERE name = the DELETE 'Djun';
. 3) modify data
general syntax:

UPDATE table_name SET field1=new-value1, field2=new-value2[WHERE Clause]
实例:

Users the SET name = the UPDATE 'Djun' the WHERE ID = 1102;
. 4) query data
general syntax:

(NOTE: * number indicates the entire query data; if the data in each column of the table are not all to be used, such as the change * id, name and other field data thus processed if large increase search speed.)

SELECT * FROM table_name [WHERE Clause];
实例:

Comma-separated list of fields to be displayed

select id, name, age from users ;
data duplication problem if large volumes of data are present, if the query does not need to duplicate records how to deal with? If you need to check how the conditions in accordance with the process? If the merchandise is repeated, the need to integrate the statistics on how the number of items of the process?

Queries do not duplicate data
general syntax:

select distinct field from table;
Example:

select distinct name from users; // Query the name is not the same user;
the SELECT DISTINCT name, from Age Students; // query a different name and age while the user
Note:

distinct must be placed at the beginning of the most
distinct fields need only be used to re-operate. ---- That is my distinct the name, age two fields, I think back to sort according to id, it is not possible because only name, age two fields operated.
When deduplication multiple fields distinct. That is: several fields simultaneously before being filtered repeated.
Conditions queries
general syntax:

select from table where field conditions;
Example:

select * from users where sex = 'M' and age> 20;
screened if desired to sort data according to the age ascending (ASC) or descending (DESC), then:

Simply add the final statement of SELECET by age ASC or by age DESC.

If you want the youngest of five out personal data only if:

select ... [limit starting offset, row number]

SELECT * from users ORDER BY age ASC limit 0,5;
data integration (aggregation)
general syntax:

select aggregate function (field) table [where Clause] [group by field1 , field2 ...] [with rollup] [having conditions];
added:

1. aggregate function (field), four conventional aggregation functions: sum (sum), count (*) (number of records), max (maximum), min (minimum).

2.group by keyword indicates to classify aggregated fields. For example, in accordance with the number of separate statistics department employees, departments should be written in the group behind by.

3.with rollup is optional grammar, indicate whether the results of the re-classification polymerization summary

4.having keyword indicates that the results of the classified conditions then filtered.

Example:

// get the user name of the user id of the same age is 1 and arranged in ascending order according to the user registration time
the SELECT username, COUNT (1) the FROM Users the GROUP BY the ORDER BY Age CreateTime the ASC;

// get the id of 1 the same user age and the number is greater than the user name 4
the SELECT username, COUNT (1) the FROM the users the GROUP BY Age the HAVING COUNT (1)> 4;

// statistics and id is the same age 1 of the user's total salary, minimum wage, maximum salary
SELECT count (1), min ( salary), max (salary) FROM users GROUP BY age;
achieve deletions change check is simply performed corresponding sql statement. Or dynamic sql statement is executed, you can be executed after a parameter to sql statement in the program assignment, execute sql realize the function.

Guess you like

Origin www.cnblogs.com/liufuyuan/p/11448360.html