What are the potential pitfalls of using MySQL in PHP projects instead of mysqli or pdo?
Potential pitfalls of using MySQL in PHP projects instead of mysqli or pdo include security vulnerabilities, lack of support for prepared statements, and limited functionality for handling transactions. To mitigate these risks, it is recommended to use mysqli or pdo extensions which offer improved security features, support for prepared statements to prevent SQL injection attacks, and better transaction handling capabilities.
// Example of connecting to a MySQL database using mysqli extension
$servername = "localhost";
$username = "username";
$password = "password";
$database = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";