Home > Backend Development > PHP Problem > How to connect php programming to mysql

How to connect php programming to mysql

Release: 2023-02-28 06:16:02
Original
1822 people have browsed it

How to connect php programming to mysql

php method to connect to mysql:

MySQLi - object-oriented

<?php
$servername = "localhost";
$username = "username";
$password = "password";
 
// 创建连接
$conn = new mysqli($servername, $username, $password);
 
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
echo "连接成功";
?>
Copy after login

MySQLi - process-oriented

<?php
$servername = "localhost";
$username = "username";
$password = "password";
 
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
 
// 检测连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "连接成功";
?>
Copy after login

Close the connection

The connection will be automatically closed after the script is executed. You can also use the following code to close the connection:

(MySQLi - Object Oriented

$conn->close();
Copy after login

MySQLi - Procedure Oriented

mysqli_close($conn);
Copy after login

Recommended: php server

The above is the detailed content of How to connect php programming to 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template