create db and user mysql and set privileges php

user1218364 picture user1218364 · Mar 15, 2012 · Viewed 46.3k times · Source

Is there a way to create a new MySQL database, a new MySQL user and give the new user privileges on the new database all using PHP?

Answer

Fabian picture Fabian · Mar 15, 2012

You could do something like this:

mysql_connect('localhost','user',password);
mysql_query("CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';");
mysql_query("GRANT ALL ON db1.* TO 'username'@'localhost'");
mysql_query("CREATE DATABASE newdatabase");
mysql_close();

You may look at the MySQL documentation on GRANT and CREATE USER