Mysql initiates the connection command and the basic syntax for connecting to the PHP program _mysql

Source: Internet
Author: User
Tags php language php mysql php script

Start a connection using MySQL binary
you can use MySQL binary to enter the MySQL command prompt to connect to the MySQL database.
Instance
The following is a simple example of connecting to a MySQL server from the command line:

[root@host]# mysql-u Root-p
Enter password:******

The mysql> Command Prompt window appears when the login succeeds, and you can execute any SQL statements on it.
After the above command executes, the login success output is as follows:

Welcome to the MySQL Monitor. Commands End With; or \g.
Your MySQL Connection ID is 2854760 to server version:5.0.9
Type ' help ' or ' \h ' for help. Type ' \c ' to clear the buffer.

In the above example, we use the root user login to the MySQL server, of course, you can also use other MySQL user login.
If the user has sufficient permissions, any user can perform SQL operations in the MySQL Command Prompt window.
You can exit the mysql> Command Prompt window by using the Exit command, as follows:

Mysql> exit
Bye


use PHP script to connect to MySQL
PHP provides the mysql_connect () function to connect to the database.
The function has 5 parameters that return the connection identity after the successful link to MySQL, and the failure returns FALSE.
Grammar

Connection mysql_connect (Server,user,passwd,new_link,client_flag);

Parameter description:


You can use PHP's Mysql_close () function to disconnect from the MySQL database.
This function has only one parameter for the mysql_connect () function to create the MySQL connection identifier returned after successful connection.
Grammar

BOOL Mysql_close (Resource $link _identifier);

This function closes a non-persistent connection to the MySQL server associated with the specified connection identity. If Link_identifier is not specified, the last open connection is closed.
Tip: You typically do not need to use mysql_close (), because an open non-persistent connection shuts down automatically after the script has finished executing.
Note: mysql_close () does not close persistent connections established by Mysql_pconnect ().
Instance
You can try the following examples to connect to your MySQL server:

 
 

PHP calls the MySQL function
MySQL can be applied in a variety of languages, including PERL, C, C + +, JAVA, and PHP. In these languages, MySQL is the most widely used web development in PHP.
Most of our examples in this tutorial are in the PHP language. If you want to know the MySQL application in PHP, you can visit our PHP using MySQL introduction.
PHP provides a variety of ways to access and manipulate MySQL database records. PHP MySQL function format is as follows:
Mysql_function (Value,value,...); The function section of the above format describes the functions of the MySQL function, such as

    • Mysqli_connect ($connect);
    • Mysqli_query ($connect, "SQL statement");
    • Mysql_fetch_array ()
    • Mysql_connect (), Mysql_close ()

The following example shows the syntax for invoking a MySQL function in PHP:

 
 

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.