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
Array"

I have NO CLUE what that means.

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

Thanks
create-db-user.php
Richard KortsBusiness Owner / Chief DeveloperAsked:
Who is Participating?

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

x
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.
0
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Dave,

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?

Thanks
0
Dave BaldwinFixer of ProblemsCommented:
Use whatever password you set up for 'root'.  Look in the 'config.inc.php' 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.
0

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:
Dave,

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?

Thanks
create-db-user-np.php
users-overview.jpg
0
Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
Dave,

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.

Thanks!
0
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.
0
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
PHP

From novice to tech pro — start learning today.