Database--a container for organizing, storing, and managing data by structure
Commonly used functions, what function to use can be checked on the manual, very convenient, the main first will be some basic can
MySQL function-(MySQL seems to be deprecated, recommended to use MYSQLI or PDO):
1. Process oriented: Connect a MySQL server--mysql_connect ($servername, $username, $passwd);
For example:
//Generate a connection
$con = mysql_connect (' localhost ', ' root ', ' 123456 ');
if (! $con) {
Die (' Connection failed! Reason for failure: '. mysql_error ());
}
// Select a database to manipulate $dbdatabase
mysql_select_db ($dbdatabase, $con);
// execute MySQL statement / / extract data $ Row=mysql_fetch_row ($result);
Using the MYSQLI function
$server = ' localhost ';
$user = ' root ';
$password = ' 123456 ';
$con = Mysqli_connect ($server, $user, $password)
Or
Die (' Connection database failed, failure reason: '. Mysqli_connect_error ());
Attention:
① using the @ (Error control operator)before functions such as mysql_connect (), mysql_select_db (), can ignore the error message generated by the system, and then we use Die () to customize the error message;
② extract data, in addition to the above mysql_fetch_row, there are MYSQL_FETCH_ASSOC and mysql_fetch_array, the specific differences please consult the PHP manual;
③ for the return value of the mysql_query () function, if the executed statement has a return value (such as SELECT, SHOW, describe, and so on), the corresponding data is returned (on success) or FALSE (on failure), if the executed statement has no return value (such as Delete, DROP, INSERT, update, and so on), returns True (on success) or FALSE (on Failure).
2, Object-oriented method-This method is very similar to the common method, but the corresponding function is replaced by the object-oriented method.
$db =New Mysqli ($server,$user,$password, $dbdatabase ); if (mysqli_connect_ Error ())
{ echo" Could not connect to database. ' exit () ; }
$result = $db->query ("Select Id,name from User" $row = $result->fetch_row ();
Here is the mysqli, meaning that the MySQL extension, either through a process-oriented approach or object-oriented way to interact with the database, the only difference is to call the function (object method) in a different way.
Method Three: Pdo method
PDO is the abbreviation for PHP database objects, which is the PHP db object . It provides a unified way for PHP to interact with various databases.
The advantage is that the basic operation of the remaining database is the same as long as the data source is provided correctly. That is, the same code can interact with MySQL, interact with SQLite3, and, of course, interact with PostgreSQL (provided you have the right data source).
To connect to the MySQL database:
$dsn = ' mysql:host= '. $dbhost. '; Dbname= '. $dbdatabasE. '; ' $dbh =new PDO ($dsn, $username, $userpass);
If it is SQLite3, use the following code directly:
$dsn = ' sqlite3: ' D:\sqlite\user.db ';
$DBH =new PDO ($DSN);
If it is PostgreSQL, the following code can deal with:
$dsn = ' pgsql:host= '. $dbhost. ' port=5432 dbname= '. $dbdatabase. ' user= '. $username. ' password= '. $userpass; $dbh =new PDO ( $DSN);
After the successful connection with the database, the following is only necessary to get data from the database or insert update data, the instance code is as follows:
$stmt = $dbh->query (' SELECT id,name from user '); $row = $stmt->fetch ();
Connection to PHP database--mysql