Home>Article>Backend Development> How to connect php and mysql?
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.
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);
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');
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.');
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' )";
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.');
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);
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!