How can PHP be used to grant user permissions to access an external database securely?

To grant user permissions to access an external database securely using PHP, you can create a separate user account in the database with limited privileges, such as only allowing SELECT queries. Then, in your PHP code, you can connect to the database using this restricted user account to ensure that users can only perform authorized actions.

<?php
$servername = "localhost";
$username = "restricted_user";
$password = "password";
$dbname = "external_db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>