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 KortsAsked:
Who is Participating?
 
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
 
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 KortsAuthor 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Richard KortsAuthor 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 KortsAuthor 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.