create user and database using php


i am wanting to create the user and the databases that that user can use and then add data to the database in an ‘install like’ script.

so far i have to create the users and databases manually via phpmyadmin, is it possible to do this using an install like script that will do this for me?

thanks in advance for any help.


just create an INSERT statement with the appropriate data and have the script insert the new user using the mysql_query (or various other methods).


<?php $db = 'database_name'; $link = mysql_connect('localhost', 'mysql_user', 'mysql_password'); if (!$link) { die('Could not connect: ' . mysql_error()); } $query = "INSERT INTO table VALUES('id','username','otherdata')"; $result = mysql_db_query($db,$query); if (!$result) { die('Invalid query: ' . mysql_error()); } [/php] The above is ONLY a simple example.



Yes this part i have, or similar, its the step before this that i wish to have done in php.

the part that creates the databases and the user, as done in phpmyadmin, can this be done in a php script.


i am wanting to create an inwill create the user and the databases. so when i lets say install the scripts on my server i do not have to go into phpmyadmin to create the users and the databases i just run the add users and databases script to do it instead.


If I understand you correctly, then the answer is YES.

You want to create a MySQL user (not just a userid for some website) that has access to a database created on demand (such as a registration script)

So if user peg110 signs up you would create a user in the mysql table along with a database (for example) called peg110 that user peg110 has full control of.

If this is correct then you can do all the same commands that you could/would do in phpMyadmin. If you notice on every command that you do in phpMyadmin, it will give you the SQL syntax. You could perform the SAME query (as adjusted by the script) for CREATE USER, CREATE TABLE, and the grants (as needed). You would then use the same mysql_db_query that you would use as in the example above.

The one thing that you need to make sure of is that the userid that you use in the mysql_connect string has the appropriate permissions to perform the create and grants that are needed.

Hope that makes sense and helps.


i got a bit lost reading that.

from the start.

i give someone the scripts that i create and to save them opening phpmyadmin and adding a user and a databases they instead run a php script that will do this for them.

re-read your last post again and see you say i can CREARE USER.
do i just user the command similar to

mysql(CREATE USER, username, password)

or something like it.


after posting the below i tested the first line to create the user and it failed…

Warning: mysql_query() [function.mysql-query]: Access denied for user ‘nobody’@‘localhost’ (using password: NO) in … file name of my script

i have just looked around again armed with this new info you provided on ‘create user’

i now have this… but i do not have the part that will create the main databases that the table will be created in. have you got any ideas?

thanks again for you help.

<? mysql_query(CREATE USER user ['password']); mysql_query("CREATE TABLE IF NOT EXISTS `table1` ( `field` varchar(255) default NULL, `value` varchar(255) NOT NULL default '0', ) ENGINE=MyISAM DEFAULT CHARSET=latin1"); ?>


WARNING: Be aware that allowing users to automatically create databases in an uncontrolled manner is a SERIOUS security issue! Unless you parse ALL user input in the correct way, they could gain access to ALL your databases and possibly delete/corrupt them! Alternatively, a malicious user could write a script/program that floods your server with users/databases, effectively maxing out your resources.

Just something you’d like to keep in mind before implementing such ‘auto creation’ scripts on your Production server :)