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:

  1. Server name: specify the hostname or IP address of the MySQL server to connect to.
  2. Username: The MySQL username specified for the connection.
  3. password: the designated MySQL password used for connection.
  4. 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.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds