BigDoug Posted April 1, 2007 Share Posted April 1, 2007 I have been banging my head on the wall for three days over this. I don't normally give up, but this is obviously above my knowledge level. I have a PHP script that as part of it's function, it creates a database within a cPanel control panel. That works fine. What I have tried to add to that is giving a specific user permissions to the database but all of my efforts have failed. Everytime I run the script with my new additions it gives me Access denied for user "userIcreated@localhost" for database "The DatabaseIcreated". I am starting to wonder if it is possible to add a user to a MySQL database programmatically when the database resides within cPanel. Does anyone know if this is possible? I want to think that something like this is very possible with PHP, I just don't have the skillset to do it. I have to believe I am close, it is just the username that seems to be wrong. Please note I AM NOT A PROGRAMMER. I just learn fast and have been reading alot. I am attaching my code for review: // cPanel variables $cpanel_user = "MyAdminUserForCpanel"; $cpanel_password = "MyPassword!"; $cpanel_host = "mydomain.net"; $cpanel_skin = "x"; // Path to cURL on your server $curl_path = "/usr/bin/curl"; if ($success != "") { //Everything OK to create database $result = exec("$curl_path 'http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/$cpanel_skin/sql/adddb.html?db=$databasekey'"); //$databasekey is defined earlier and works fine. The database gets created here just fine. } else { echo 'There was an error creating your databse information . The System Administrator has been notified'; echo mysql_error(); }//End the database creation routine and move on to inserting privileges for the PROD user to access the database. // This file contains the database access information. This file also establishes a connection to MySQL and selects the database. //Set the database access information as constants. DEFINE ('DB_USER', 'MyAdminUserForCpanel'); DEFINE ('DB_PASSWORD', 'MyPassword!'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', "$databasekey"); if ($dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)) { // Make the connnection. if (!mysql_select_db (DB_NAME)) { // If it can't select the database. // Handle the error. echo 'Could not select the database: ' . mysql_error(); // Print a message to the user, include the footer, and kill the script. echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; exit(); } // End of mysql_select_db IF. } else { // If it couldn't connect to MySQL. mysql_select_db(DB_NAME); @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); $query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv, delete_priv, create_priv, drop_priv, index_priv, alter_priv, references_priv) VALUES ('localhost', 'MyAdminUserForCpanel', 'PASSWORD(MyPassword!')', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'"; $result = @mysql_query ($query) or die(mysql_error()); if (mysql_affected_rows() == 1) { mysql_close(); } // End of $dbc IF. } Quote Link to comment https://forums.phpfreaks.com/topic/45160-adding-users-to-mysql-database-with-php-and-cpanel/ Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 'PASSWORD(MyPassword!')' is that correct ? Quote Link to comment https://forums.phpfreaks.com/topic/45160-adding-users-to-mysql-database-with-php-and-cpanel/#findComment-219219 Share on other sites More sharing options...
BigDoug Posted April 1, 2007 Author Share Posted April 1, 2007 No, I typo'd it. It is: 'PASSWORD('MyPassword!') Quote Link to comment https://forums.phpfreaks.com/topic/45160-adding-users-to-mysql-database-with-php-and-cpanel/#findComment-219226 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 well you better check it in the $query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv, delete_priv, create_priv, drop_priv, index_priv, alter_priv, references_priv) VALUES ('localhost', 'MyAdminUserForCpanel', 'PASSWORD(MyPassword!')', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'"; Quote Link to comment https://forums.phpfreaks.com/topic/45160-adding-users-to-mysql-database-with-php-and-cpanel/#findComment-219229 Share on other sites More sharing options...
BigDoug Posted April 1, 2007 Author Share Posted April 1, 2007 Yes, I have it correct in my production code, I just typo'd it when I cut/pasted here. Quote Link to comment https://forums.phpfreaks.com/topic/45160-adding-users-to-mysql-database-with-php-and-cpanel/#findComment-219240 Share on other sites More sharing options...
BigDoug Posted April 1, 2007 Author Share Posted April 1, 2007 I want to note that I am on a shared reseller hosting account and as such do not have ROOT MySQL access. I just assumed because I can create the database programmatically that I could add users to the database at the same time, but maybe not. Anyone have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/45160-adding-users-to-mysql-database-with-php-and-cpanel/#findComment-219248 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 Well from what i see you have alot of diffent "typ'os" until they are cleared, i'm not sure what to point out Quote Link to comment https://forums.phpfreaks.com/topic/45160-adding-users-to-mysql-database-with-php-and-cpanel/#findComment-219252 Share on other sites More sharing options...
BigDoug Posted April 1, 2007 Author Share Posted April 1, 2007 Well from what i see you have alot of diffent "typ'os" until they are cleared, i'm not sure what to point out This doesn't help me much. Care to elaborate what you see that is a typo or error besides what I already said is correct in my "real" code I am using? Quote Link to comment https://forums.phpfreaks.com/topic/45160-adding-users-to-mysql-database-with-php-and-cpanel/#findComment-219285 Share on other sites More sharing options...
MadTechie Posted April 1, 2007 Share Posted April 1, 2007 maybe posting your real code will reveal real problems just another one from the same line as the one above <?php $query = "INSERT INTO user (host, user, password, select_priv, insert_priv, update_priv, delete_priv, create_priv, drop_priv, index_priv, alter_priv, references_priv) VALUES ('localhost', 'MyAdminUserForCpanel', 'PASSWORD(MyPassword!')', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'"; ?> missing the close bracket ")" Quote Link to comment https://forums.phpfreaks.com/topic/45160-adding-users-to-mysql-database-with-php-and-cpanel/#findComment-219287 Share on other sites More sharing options...
BigDoug Posted April 1, 2007 Author Share Posted April 1, 2007 I fixed that (good catch BTW), but the same error persists. I am calling it quits for now. 50 hours spent on this and now I just keeping making mistakes. Quote Link to comment https://forums.phpfreaks.com/topic/45160-adding-users-to-mysql-database-with-php-and-cpanel/#findComment-219327 Share on other sites More sharing options...
BigDoug Posted April 1, 2007 Author Share Posted April 1, 2007 Just an update for anyone interested. I finally muddled through this on my own and have discovered that outside of the Cpanel interface, I do not have sufficient privileges to grant users privileges on databases. cPanel must use root MySQL access to accomplish this. I will have to regroup and rethink my strategy. Quote Link to comment https://forums.phpfreaks.com/topic/45160-adding-users-to-mysql-database-with-php-and-cpanel/#findComment-219437 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.