How can the mysql_error() function be used to troubleshoot PHP MySQL database connection issues?
When troubleshooting PHP MySQL database connection issues, the mysql_error() function can be used to display any error messages generated by MySQL. This can help identify the specific problem with the database connection, such as incorrect credentials or server issues. By capturing and displaying these error messages, developers can quickly diagnose and address the root cause of the connection problem.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
Related Questions
- How can the use of arrays improve the readability and efficiency of code for quarterly calculations in PHP?
- What are the potential security risks associated with using the mysql_connect function in PHP for database connections?
- How can developers optimize PHP code for efficient text processing and manipulation?