A. php to connect to the database steps:
1 Establish a connection with the database:
$conn = mysql_connect ("localhost:3306", "username", "pass");
Three parameters: The first is the database server hostname and port localhost:3306, if the host name or port defaults will use the value set in PHP.ini, the following two are connected to the database server's username and password, the same defaults to the value set in PHP.ini.
2 Submit the query statement:
A. Use function mysql_select_db () to select the database to access
eg
mysql_select_db ("MyDB");
The function returns the logical value TRUE or FALSE based on the success or failure of the operation.
B. Executing SQL statements
Using the function mysql_query (), there are two parameters, the first parameter is an SQL statement, and the second parameter is the connection number generated by the mysql_connect () function.
eg
$result =mysql_query ("SELECT * From Address", $conn);
When a function submits an SQL statement, the query results are generated and the result number is returned.
3 Return the content in the result
two. mysql Common commands
1. Start MySQL
Mysql-u root-p
2. Change Password:
Mysqladmin-u username-p Old password password new password
3. Display command
Copy Code code as follows:
show databases;
Use MyDB;
Show tables;
Describe TableName;
Create database DatabaseName;
CREATE TABLE TableName;
drop TABLE TableName;
Drop database databasename;
Delete from TableName;
SELECT * FROM TableName;
INSERT INTO TableName values (', ');
4. Import MySQL from file
Mysql-u Root-p </usr/local/info/mydb.sql (where/usr/local/info/mydb.sql is the database file storage directory)
5. Transferring text data to a database
The format that text data should conform to: field data is separated by a TAB key, and null values are replaced with \\n.
eg
3 Rose Shenzhen II 1976-10-10
Incoming command load data local infile \ filename \ into table name
Note: It is a good idea to copy the files to the/mysql/bin directory and open the library with the use command first.
6. Time format
Using DateTime to declare time variables in MySQL database
eg
Time datetime; Its input data format: 1985-06-01 12:20:50
Get system time in PHP (in YYYY-MM-DD HH:MM:SS format):
$t =time ();
$time =date (' y-m-d h:i:s ', $t);
7. Unique Specify unique attributes