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 code structure be optimized to adhere more closely to the principles of the MVC pattern in PHP?
- What are the advantages and disadvantages of using JSON over PHP for generating user feeds?
- What are the potential pitfalls of using htmlentities() and stripslashes() in PHP for processing text input?