Database: Stored as a file (2 binary)
SQL must define character set UTF8
1. Connecting Mysql_connect
2. Select DB Mysql_ select_db
3. Assemble SQL and send execution mysql_query (return format type query class returns the rest of the resource returns TRUE or FALSE)
4. Processing Results Mysql_fetch_array
5. Close Mysql_close
Method One: Common method (Process oriented)
First, I first make the following assumptions (also applicable with method two and method three)
$username =your_name, $userpass =your_pass, $dbhost =localhost; $dbdatabase =your_database;
Here are the key steps:
1//Generate a Connection 2 $db _connect=mysql_connect ($dbhost, $username, $userpass) or die ("Unable to connect to the mysql!"); 3 4//Select a database to operate 5 mysql_select_db ($dbdatabase, $db _connect); 6 7//execute MySQL statement 8 $result =mysql_query ("Select Id,name from user "); 9 10//Extract Data $row =mysql_fetch_row ($result);
The code comment already explains everything. But there are some additions to be made here.
① 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 when, in addition to the above mysql_fetch_row, common there are mysql_fetch_assoc and mysql_fetch_array, the specific differences please check 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).
Method Two: Object-oriented approach
In fact, this method is very similar to the normal method, just replace the corresponding function with the object-oriented method, directly look at the code.
1 $db =new mysqli ($dbhost, $username, $userpass, $dbdatabase); 2 if (Mysqli_connect_error ()) {3 echo ' Could not connect to database. '; 4 exit;5}6 7 $result = $db->query ("Select Id,name from User"), 8 $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 actually the abbreviation for PHP database objects, which is the PHP object in Chinese. It provides a unified way for PHP to interact with the database.
This is a popular way to connect a database at present. The advantage of this is that the basic operation of the database is the same as long as the data source is provided correctly. That is, the same piece of code can either interact with MySQL, interact with SQLite3, and, of course, interact with PostgreSQL, provided you have the right data source. Here's a look at the code that connects MySQL:
$dsn = ' mysql:host= '. $dbhost. '; Dbname= '. $dbdatabase. '; ' $DBH =new PDO ($dsn, $username, $userpass);
If it is SQLite3, use the following code directly:
$dsn = ' sqlite3: ' C:\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 ();
Database connection PHP