Example of connection and shutdown of MySQL database

Source: Internet
Author: User
Tags dsn mysql client mysql in php database postgresql stmt

The MySQL client is primarily used to pass SQL queries to the server and displays the results after execution. It can run on the same machine as the server, or on two machines on the network. When you connect to a MySQL server, your identity is determined by the host you connect to and the username you specify. So MySQL in the identification of the identity will consider your host name and login user name, only the client host is granted permission to connect to the MySQL server. After you start the operating system command, you can use the following command to connect to the MySQL server:

Mysql-h Server host address-u user name-P user Password

The meaning of each parameter is shown below.
★-h: Specifies the location of the connected database server, either an IP address or a server domain name.
★-u: Specifies the user name to use to connect to the database server, for example, Root has all permissions for the administrator user.
★-p: Connect the password used by the database server, but do not have spaces between-p and subsequent arguments. Finally, enter the password directly after the parameter, then in the form of ciphertext.
The MySQL data server can be managed by entering SQL query statements in the console and sending them. and each command to end with a semicolon, if you enter the command, after the carriage return found that forgot to add a semicolon, you do not need to repeat the command, as long as a semicolon hit the ENTER key on the line. That is to say, you can divide a complete command into lines and then use a semicolon as the closing sign. You can also use the cursor up and down keys to pull up previous commands.
If you want to quit the client, you can enter the exit or quit command at any time in the console to end the call.

Method One: Common method (Process oriented)

First, let me make the following assumptions (also applicable with method two and method III)

$username =your_name;
$userpass =your_pass;
$dbhost =localhost;
$dbdatabase =your_database;

Here are the key steps:

The code is as follows Copy Code


Generate a connection
$db _connect=mysql_connect ($dbhost, $username, $userpass) or die ("Unable to connect to the mysql!");

Select a database to operate on
mysql_select_db ($dbdatabase, $db _connect);

Execute MySQL Statement
$result =mysql_query ("Select Id,name from user");

Extract data
$row =mysql_fetch_row ($result);

Code comments have explained everything. But there is a need to make some additions.

① uses the @ (error control operator) before functions such as mysql_connect (), mysql_select_db (), and can ignore the error messages generated by the system, and then we use Die () to customize the error message;

② extraction of data, in addition to the above mysql_fetch_row, common also MYSQL_FETCH_ASSOC and mysql_fetch_array, the specific difference please consult 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 (when it succeeds) or false (when it fails), if the executed statement does not have a return value (such as Delete, DROP, INSERT, update, and so on), returns True (when it succeeds) or false (when it fails).

Method Two: Object-oriented method

In fact, this method is very similar to the common method, but instead of the corresponding function to the Object-oriented method, directly look at the code.

The code is as follows Copy Code

$db =new mysqli ($dbhost, $username, $userpass, $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 the extension of MySQL, can also be in a process-oriented way to interact with the database in an object-oriented manner, the only difference is the way to invoke the function (object method).

method Three: Pdo method

PDO is actually the abbreviation of PHP database objects, the Chinese is the PHP object. It provides a unified way for PHP to interact with the database.

This is a popular way to connect to the database at present. Its advantage is that as long as the data source is provided correctly, the rest of the basic operation of the database is the same. That is, the same piece of code can interact with MySQL, interact with SQLite3, and, of course, interact with PostgreSQL, provided you provide the right data source. Here's a look at the code to connect to MySQL:

The code is as follows Copy Code

$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 a successful connection to the database, you only need to get the data from the database or insert the updated data, as shown in the following instance code:

$stmt = $dbh->query (' SELECT id,name from user ');
$row = $stmt->fetch ();

Releasing resources

After you complete the SQL operation, you must free up the connection resources that you have established to avoid excessive connection occupancy resulting in degraded system performance. The Release resources directive is as follows:

The code is as follows Copy Code


Mysql_free_result ($result);
Mysql_close ($link);

In both lines, the first line releases the variable $result, and the second line closes the connection to the database $link to free up the memory space and the database connection.

To release a resource directive:

  code is as follows copy code

Mysql_free _result ($result);

Mysql_close ($link);

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.