How to grant privileges to a mysql user by using php?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mehtab_hu
Forum Newbie
Posts: 2
Joined: Thu Nov 06, 2008 6:48 am

How to grant privileges to a mysql user by using php?

Post by mehtab_hu »

How to grant privileges to a mysql user by using php.
I work on a php project where I can
1: Create database by using php script.
2: Create user in mysql by using php script.
3: Grant access to user for the specific database by using php script.
Problem is in step no 3.
I need to know how can grant access to user for a specific database by using php script, where the user has full access control on has database but the other database should not accessible.

Need some privileges techniques overcome the above problem.
If any one have knowledge about the privileges then please help me!
Thank
Best regard
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How to grant privileges to a mysql user by using php?

Post by onion2k »

It's just an SQL query like any other. Use mysql_query("GRANT blah blah blah").. Obviously the user you connect to MySQL with will need to have the GRANT privilege.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How to grant privileges to a mysql user by using php?

Post by VladSun »

In fact, using the GRANT query will create the user specified if it's not existing. So Step 2 can be omitted.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How to grant privileges to a mysql user by using php?

Post by onion2k »

VladSun wrote:In fact, using the GRANT query will create the user specified if it's not existing. So Step 2 can be omitted.
I'd still do it anyway otherwise the user will be created without a password..
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: How to grant privileges to a mysql user by using php?

Post by VladSun »

I don't think so. At least it will not happen with a proper GRANT query:
[sql] GRANT    ALLON    mydatabase.*TO    myuser@myhostIDENTIFIED BY    'mypassword' [/sql]
There are 10 types of people in this world, those who understand binary and those who don't
mehtab_hu
Forum Newbie
Posts: 2
Joined: Thu Nov 06, 2008 6:48 am

Re: How to grant privileges to a mysql user by using php?

Post by mehtab_hu »

Hi VladSun & onion2k!
Thanks for helping!
My script is working now.
Best Regard.
Post Reply