Results 1 to 9 of 9

Thread: [RESOLVED] Changing Privileges via code

  1. #1

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Resolved [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

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    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.

  3. #3
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: Changing Privileges via code

    You can also use "REVOKE" (in place of "GRANT") with similar syntax.

  4. #4

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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:

    source: http://www.trap17.com/index.php/Addi...in_t31803.html
    GRANT privileges
    ON databasename.tablename
    TO username
    Identified by 'password';

    example:

    GRANT SELECT, INSERT //or GRANT ALL to grant all privileges
    ON DB1.*
    TO AMEZIS
    IDENTIFIED BY 'PIZZA';
    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

  5. #5

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    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

  6. #6
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    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().

  7. #7

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Changing Privileges via code

    Quote Originally Posted by SambaNeko View Post
    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

  8. #8
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    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.

  9. #9

    Thread Starter
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Changing Privileges via code

    Quote Originally Posted by SambaNeko View Post
    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
  •  



Click Here to Expand Forum to Full Width