Home >Database >Mysql Tutorial >How to Fix 'Failed to Connect to MySQL' Error in PHP?
Connecting to MySQL Database in PHP with mysqli
While connecting to a database on a web server, an error message stating "Failed to connect to MySQL" can arise. This article addresses this issue and provides solutions.
The provided code connects to a database using mysqli_connect, but lacks the required server name parameter.
解决方案 1:
mysqli_connect("localhost","username" ,"password","databasename");
In this case, "localhost" represents the server name.
解决方案 2 (mysqli Procedural):
$servername = "localhost"; $username = "username"; $password = "password"; $con = mysqli_connect($servername, $username, $password);
This approach uses separate variables for server name, username, and password.
解决方案 3:
In some cases, the server may use the root user with an empty password.
$servername = "localhost"; $username = "root"; $password = "";
注意: Ensure that you use the correct server name, username, and password for your database configuration.
The above is the detailed content of How to Fix 'Failed to Connect to MySQL' Error in PHP?. For more information, please follow other related articles on the PHP Chinese website!