How can PHP be used to create user accounts with specific permissions for database access?

To create user accounts with specific permissions for database access in PHP, you can use SQL queries to create a new user in the database with the desired permissions. You can then use PHP to connect to the database and execute these SQL queries to create the user account with the necessary permissions.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDB";

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

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

// SQL query to create a new user with specific permissions
$sql = "CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password'";
$sql .= "GRANT SELECT, INSERT, UPDATE, DELETE ON myDB.* TO 'newuser'@'localhost'";

// Execute the SQL query
if ($conn->multi_query($sql) === TRUE) {
    echo "User account created successfully";
} else {
    echo "Error creating user account: " . $conn->error;
}

// Close connection
$conn->close();
?>