Im trying to connect to mysql database through php but it gives the following error (Connection failed: could not find driver). I am using xampp and windows 11. I am new to this so im not sure if this is enough info
Here’s my code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mhdatabase";
try {
// Connect to the database using PDO
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// Set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Prepare SQL query
$sql = "SELECT * FROM resourceslist";
// Execute query
$stmt = $conn->query($sql);
// Fetch all rows into an associative array
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
exit();
}
?>
<?php
// Close the PDO connection
$conn = null;
?>
New contributor
Pravinya Valluri is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.