MySQL uses the C/S structure, and mysqlcommandlineclient acts as a client. If you use a PHP script to process MySQL, PHP acts as the MySQL client. Note: MySQL is not an integrated component of PHP, but an extension component. it must be enabled in php. ini.
MySQL uses the C/S structure, and the mysql command line client acts as a client. If you use a PHP script to process MySQL, PHP acts as the MySQL client. Note that MySQL is not an integrated component of PHP, but an extension component that must be enabled in php. ini.
Three methods for connecting PHP to MySQL:
- Mysql extension Library
- Mysqli Extension Library
- PDO
PHP can process most databases
PHP + MySQL, regarded as a golden partner
Of course, we can still use other databases.
PHP connection to mysql
// Php connection to mysql
1. establish a connection from the client to the server
$ Conn = mysql_connect ("localhost", "root", "123456 ");
// Var_dump ($ conn );
2. select the database mysql_select_db. The first parameter is the database name, and the second parameter is the connection identifier. this parameter can be omitted. If this parameter is omitted, it will find the last opened connection. Return a value, bool,
Mysql_select_db ("wcp", $ conn)
3. execute the corresponding SQL command:
Mysql_query ($ SQL, $ conn)
// 4. close the connection
Mysql_close ($ conn );
The executed operations include CRUD.
Create, select, update, delete, insert
// Set the character set to avoid garbled characters
Mysql_query ("set names utf8 ");
Tip: // error message
Mysql_error ()
// Error message number
Mysql_errno ()
1. correctly process the data transmitted from the form. the format is valid, the range is valid, and there are spaces and special characters. String processing is required.
① Trim function, remove the left and right spaces.
② The TIME obtained by the form is a string, which must be converted to a timestamp.
③ Strtotime () directly converts the string into a timestamp
④ SQL statements should be written in double quotation marks. Variables are enclosed by single quotes
2. the resource identifier returned by the Select query. if the returned result is false due to an error, the returned resource is processed.
Mysql_fetch_array () contains the associated array of the index array
Mysql_fetch_row () retrieves the index array
Mysql_fetch_assoc () takes only the associated array
Mysql_fetch_object () is obtained as an object
Only one query will be performed in the database at a time.