Is it advisable to use MySQL functions like mysql_connect in PHP to manage database connections?
It is not advisable to use MySQL functions like mysql_connect in PHP to manage database connections as they are deprecated as of PHP 5.5.0 and removed in PHP 7.0.0. Instead, it is recommended to use the MySQLi or PDO extensions for connecting to a MySQL database in PHP.
// Using MySQLi extension to connect to a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
Related Questions
- What are the potential pitfalls of using JOIN in PHP queries?
- In what scenarios should hidden fields be used in PHP form processing to trigger specific actions based on user input, and how can this be implemented effectively?
- What are common best practices for password validation in PHP, specifically in terms of length and character requirements?