Create MySQL DB User / Database

I want to set up a php program that will create a MySQL database & a MySql database user for that database.

I know how to do this in phpMyadmin; I have done it a hundred times.

For reasons I don't need to explain here, I want a php script, like create_db_user.php that will create a database & a user with a password. Of course I want that user to have privileges too, I'll cross that bridge later.

Everything I find on the web that shows how to create a database assumes the existence of a User and a password, but I can find NO WAY to create a user & a password without a database. So it's like the chicken & the egg.

See attached. I ran this, it did nothing and output this: "Notice: Array to string conversion in C:\xampp\htdocs\seba\create_db_user.php on line 8

I have NO CLUE what that means.

I would like CLEAR & CONCISE steps on how to do this from within a php program.

Richard KortsBusiness Owner / Chief DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
phpMyAdmin uses either 'root' or a highly privileged MySQL user to do admin activities.  You can NOT create anything without a valid MySQL user.  And to use 'root', you normally have to be running on the server where the MySQL server is running.  'root' is not normally allowed from remote access.

Your code does not have a connection function along with proper username and password to access the MySQL server.  Note also that if you create a user with 'localhost', they will not be able to connect remotely to the MySQL server.
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:

I am running XAMPP, which is a Apache / MySQL / php environment for Windows.

In this instance, I do NOT want to connect to a remote host, just localhost.

So can I use user 'root' to do this? How do I know what to use for a password?

Dave BaldwinFixer of ProblemsCommented:
Use whatever password you set up for 'root'.  Look in the '' file in the 'phpmyadmin' directory, it is listed there.  Do Not change anything there.  That username and password corresponds to the entry in the MySQL 'user' table.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:

I accidentally found out how to change the root password, which I did.

Then I ran the attached script (in the script attached I removed the "real" root password).

It created the database & the user. Yea!!

But on the user, note the attached jpg users_overview, it assigned host % rather than localhost???

What does % mean?

Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:

I think I know why the user was setup as it was, I was granting privileges to a user without first creating that user.

So I think I see it all now.

Dave BaldwinFixer of ProblemsCommented:
As usual, it did what you told it to.  You should get rid of the 'Any' users.  They don't have a password and 'Any'one can connect by using them.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.