What is the usage of mysql connect?
In MySQL, you can use the MySQL Connect function to connect to a MySQL database. The basic syntax of the MySQL Connect function is as follows:
mysqli_connect(servername, username, password, dbname);
The meaning of the parameters are as follows:
- Server name: specify the hostname or IP address of the MySQL server to connect to.
- Username: The MySQL username specified for the connection.
- password: the designated MySQL password used for connection.
- Database name: specifies the name of the database to connect to.
The sample code is provided below:
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "mydatabase";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
echo "连接成功";
// 关闭连接
mysqli_close($conn);
The above sample code uses the mysqli_connect function to connect to a MySQL database, checks for successful and failed connections, and finally closes the connection.