Create Mysql user and database via PHP

Asked

Viewed 528 times

1

1 - New Mysql user and database can be created via PHP script?

2 - If yes, it would involve some security risk such action?

Objective: I have a running system (own CMS) where the user does practically everything to activate on the server, and, this will be allocated in a subdomain on our server, therefore, the creation of a database for this new user for this new subdomain would need to be automated at the time of registration, which today does not occur and every time we have to create new user and database for a new registration. I accept suggestions if you have other ways.

Details: VPS running Linux / WHM(11.52.0) / cPanel(11.52.0.15) / Mysql(5.6.23) / PHP(5.5)

2 answers

2

1 - Yes if the user that the php script runs has "GRANT OPTION", he can create other users and give permissions, from nothing until his equivalent permission.

2 - As it is said, the chain is as strong as the weakest link, it depends on how your php is mounted. Other than that everything is perfectly possible the way you intend.

  • Marcelo, your answer cleared part of the doubt, but in your answer did not mention about creating database, but only create users, know if with GRANT OPTION this is also possible? I’m using the cPanel class itself.

-2

I did so:

<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <meta charset="UTF-8" content="viewport">
        <meta name="viewport" content="width=device-width">
        <title>Criando Banco de Dados...</title>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" media="screen">
    </head>
<body>
<div class="container">
    <form action="" method="post">
        <fieldset form="formcreatedb">
            <legend>Criando Database</legend>
                <input name="nome" class="fcampo" required maxlength="50">
                <input type="submit" class="button" value="Criar Banco..." ><br><br>

        </fieldset>

    </form>
</div>
</body>
</html>
<?php

$nome = $_POST['nome'];

// Create database
$sql = "CREATE DATABASE IF NOT EXISTS $nome;";
if (mysqli_query($conn, $sql)) {
    echo "Banco";
} else {
    echo "Error creating database: " . mysqli_error($conn);
}

mysqli_close($conn);

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.