|
-
Oct 19th, 2009, 08:00 AM
#1
[RESOLVED] Changing Privileges via code
Hi,
This question follows on from this thread but it's about the code rather than the program.
Is it possible to change the user privileges of a database via code rather than having to manually add or adjust them?
Thanks,
Nightwalker
Last edited by Nightwalker83; Oct 22nd, 2009 at 11:37 AM.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Oct 19th, 2009, 09:43 AM
#2
Re: Changing Privileges via code
you can run queries to grant privileges if you need to, though if you're running on a shared host this may not be possible.
you will have to run the two following SQL queries:
GRANT select,insert,update,delete,create,drop ON database.* TO username@localhost identified BY 'password';
FLUSH PRIVILEGES;
note that the host (localhost) might need to be changed, or can be set to any host by setting it to "%" (including the quotes -- though, this is not exactly advised). and you can specify which tables this user has these privileges on by removing the "catch all":
GRANT select ON database.table_name TO username@localhost identified BY 'password';
make sure you flush your privileges regardless of what you do, assuming it all works. you probably need a root database account to be able to do this.
-
Oct 19th, 2009, 11:45 AM
#3
Re: Changing Privileges via code
You can also use "REVOKE" (in place of "GRANT") with similar syntax.
-
Oct 19th, 2009, 06:42 PM
#4
Re: Changing Privileges via code
This is the script I will be using to create the database. Although, there are 2 or 3 other scripts that not only add the data to the database but check to see if the table and fields needed at the time are available before adding the data and create them if they are not.
Edit:
I found an example of what kows said:
Is it also possible to create a new user via code? I have searched on google but couldn't find any info about creating a new phpadmin user via code.
Edit:
Yes it is! One way to do it is add a user to the mysql user table. Here is the source of the info and below is a test script I wrote.
PHP Code:
<?php // Database connection variables $dbDatabase = "mysql";
//connect to db $conn = mysql_connect("localhost", "root", ""); if (!$conn) { die("Connection failed: " .mysql_error()); }
//select database if (mysql_select_db($dbDatabase, $conn)) { echo ("Database selection successful!"); }else { die ("Could not locate test database" .mysql_error()); }
//insert data into tables $insert = "INSERT INTO user ( Host, User, Password, Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv, Reload_priv, Shutdown_priv, Process_priv, File_priv, Grant_priv, References_priv, Index_priv, Alter_priv, Show_db_priv, Super_priv, Create_tmp_table_priv, Lock_tables_priv, Execute_priv, Repl_slave_priv, Repl_client_priv, Create_view_priv, Show_view_priv, Create_routine_priv, Alter_routine_priv, Create_user_priv, Event_priv, Trigger_priv, ssl_type, max_questions, max_updates, max_connections, max_user_connections ) VALUES ( 'local', 'admin', '', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', '', '0', '0', '0', '0' )"; if (mysql_query($insert, $conn)) { echo ("Insert query successful!"); }else { die ("Database query failed: " .mysql_error()); $query ="FLUSH PRIVILEGES"; } ?>
Last edited by Nightwalker83; Oct 22nd, 2009 at 08:52 PM.
Reason: Adding more
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Oct 22nd, 2009, 11:41 AM
#5
Re: Changing Privileges via code
Can someone tell me why the above php code is only working some of the time when I run it from a script? If I paste the sql into phpmyadmin it works ok.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Oct 22nd, 2009, 12:28 PM
#6
Re: Changing Privileges via code
mysql_query() doesn't support multiple queries - you've got an INSERT and a FLUSH, you can only do one at a time through mysql_query().
-
Oct 22nd, 2009, 04:36 PM
#7
Re: Changing Privileges via code
 Originally Posted by SambaNeko
mysql_query() doesn't support multiple queries - you've got an INSERT and a FLUSH, you can only do one at a time through mysql_query().
Ah ok! Can you tell me what I flush does and how to use it? I assume that a flash wipes everything so that you can reuse the tables, etc without having to recreate them?
Last edited by Nightwalker83; Oct 22nd, 2009 at 04:51 PM.
Reason: Fixed spelling
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Oct 22nd, 2009, 05:40 PM
#8
Re: Changing Privileges via code
MySQL FLUSH
FLUSH does not delete anything from your tables, it "clears or reloads various internal caches used by MySQL." (see the link above for more info) If you want to clear out all rows of a table, you'd use TRUNCATE.
-
Oct 22nd, 2009, 08:57 PM
#9
Re: Changing Privileges via code
 Originally Posted by SambaNeko
MySQL FLUSH
FLUSH does not delete anything from your tables, it "clears or reloads various internal caches used by MySQL." (see the link above for more info) If you want to clear out all rows of a table, you'd use TRUNCATE.
Thanks! I edited my code in post #4 with the new code which works. I noticed in the old code that there was a small typo I was missing a ")" as well as the flush.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|