Home > Backend Development > PHP Tutorial > How to connect php and mysql?

How to connect php and mysql?

little bottle
Release: 2023-04-06 15:42:02
Original
9723 people have browsed it

Steps for connecting php to mysql: 1. Use mysqli_connect() to connect to the database; 2. Create a PHP string variable, which contains the SQL query statement to be executed; 3. Use mysqli_query() to execute the query; 4. Use mysqli_close() to close the connection.

How to connect php and mysql?

The steps used to connect the two are mainly divided into four steps:

1. Use mysqli_connect( ) to connect to the database;

2. Assemble the SQL query string and put it into a variable, which will be used as a required parameter in the next step;

3. Use mysqli_query() to execute the query,

4. Use mysqli_close() to close the connection.

In-depth analysis below

1. mysqli_connect(): Establish a connection, syntax

 mysqli_connect(server,user,passwd,database_name);
Copy after login

This function combines the location, user name, password, and database name It is processed as a string, so it must be enclosed in quotation marks, for example:

$dbc = mysqli_connect('localhost','root','password','aliendatabase');
Copy after login

Calling this function can get a database connection, and also get a PHP variable $dbc, which interacts with the database.

Note: 1. PHP statements must be ended with a semicolon.

2. Using 'localhost' means that the database server and the Web server are on the same server computer.

3. If the fourth parameter is omitted, mysqli_select_db() will be used to specify the database name;

4. If the connection fails, the die() function will terminate the PHP script and provide a failure code Feedback, such as

$dbc = mysqli_connect('localhost','root','password','aliendatabase') or die('Error connecting to MySQL sever.');
Copy after login

If the connection fails to be created, call the die() function, and the feedback message will be output to the page. At the same time, note that there is no need for a semicolon between the two functions (that is, before or), because this It is a continuation of the same statement.

2. Create a PHP string variable, which contains the SQL query statements to be executed, such as add, delete, create, etc. For example, create an INSERT query:

$query = "INSERT INTO aliens_abduction(first_name, last_name, ".
        " when_it_happend, how_long, how_many, alien_description, ".
        " what_they_did ,fang_spotted, other, email) ".
        "VALUES ('Sally', 'Jones', '3 days ago', '1 day',’four‘, ".
        " 'green with six tentacles', 'We just talked and palyed with a dog' , ".
        " 'yes', 'I may have seen your dog .Contact me.', ".
        " 'sa'
    )";
Copy after login

Code description: 1. $query is a PHP string variable that now contains an INSERT query;

2. The period "." tells PHP to concatenate this string with the next line;

3. The entire code is PHP code , must end with a semicolon.

After the INSERT query is stored in a string, it can be passed to the mysqli_query() function

Note: 1. The meaning of "query" is quite broad and can refer to any SQL completed on the database Commands, including storing and retrieving data;

2. SQL query statements in PHP use double quotes

3. String values ​​in SQL query statements must be quoted

4. Numerical values ​​do not require quotation marks

5. NULL values ​​do not require quotation marks

3. Use PHP to query the MySQL database

The mysqli_query() function requires two pieces of information to complete Query: database connection (first step) and SQL query string (second step), for example:

$result = mysqli_query($dbc,$query);
        or die('Error querying database.');
Copy after login

The $result variable only stores whether the query executed by mysqli_query() is successful.

IV. Use mysqli_close() to close the connection. The parameter is the database connection variable of the initial resume, for example:

mysqli_close($dbc);
Copy after login

The above is the detailed content of How to connect php and mysql?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template