What is the correct way to establish a MySQL connection in PHP?
To establish a MySQL connection in PHP, you need to use the mysqli_connect() function. This function takes four parameters: the hostname (usually "localhost"), the username, the password, and the database name. Once the connection is established, you can perform various database operations using this connection.
// Establishing a MySQL connection
$hostname = "localhost";
$username = "root";
$password = "";
$database = "my_database";
$conn = mysqli_connect($hostname, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
Keywords
Related Questions
- In what scenarios would encrypting passwords using functions like MD5 be sufficient for securing PHP applications?
- What are the potential security risks associated with using placeholders in SQL queries in PHP?
- What advice would you give to a beginner looking to create a professional WebShop in PHP and MySQL?