MySQL common commands and learning experience _mysql

Source: Internet
Author: User
Tags datetime create database
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
Related Article

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.