Jump to content

[SOLVED] Problem creating a MySQL user via PHP


jaxdevil

Recommended Posts

I have a code snippet which is supposed to create a database, add a user to MySQL, and add that user to the new database with all privileges. I am copying it below. It is creating the new database, but not creating the user or adding it to the database. Can anyone tell what I am doing wrong on there? Oh yeah, the final $result I added is to a page that is a seperate query altogether that inserts the tables and data, but that is run last so I don't think that is affecting the user creation. Here is the code (I changed the username, password, and URL to dummy data):

 

###############################################################
# cPanel Database Creator 1.2
###############################################################
# Visit http://www.zubrag.com/scripts/ for updates
############################################################### 

// cPanel username (you use to login to cPanel)
$cpanel_user = "username";

// cPanel password (you use to login to cPanel)
$cpanel_password = "xxxxxx";

// cPanel domain (example: mysite.com)
$cpanel_host = "somedomain.com";

// cPanel theme/skin, usually "x"
// Check http://www.zubrag.com/articles/determine-cpanel-skin.php
// to know it for sure
$cpanel_skin = "x";

// Script will add user to database if these values are not empty
// User wil have ALL permissions
$db_name = $u['login'];
$db_username = $u['login'];
$db_userpass = 'xxxxxx';

// Update this only if you are experienced user or if script does not work
// Path to cURL on your server. Usually /usr/bin/curl
$curl_path = "/usr/local/bin/curl";

//////////////////////////////////////
/* Code below should not be changed */
//////////////////////////////////////

function execCommand($command) {
  global $curl_path;

  if (!empty($curl_path)) {
    return exec("$curl_path '$command'");
  }
  else {
    return file_get_contents($command);
  }
}

  // escape db name
  $db_name = escapeshellarg($db_name);

  // will return empty string on success, error message on error
  $result .= execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/x/sql/adddb.html?db=$db_name");

    // create user
    $result .= execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/x/sql/adduser.html?user=$db_username&pass=$db_userpass");

    // assign user to database
    $result .= execCommand("http://$cpanel_user:$cpanel_password@$cpanel_host:2082/frontend/x/sql/addusertodb.html?user=$cpanel_user_$db_username&db=$cpanel_user_$db_name&ALL=ALL");

// insert tables and data 
    $result .= execCommand("http://$cpanel_host/members/dbstructure.php?acct=$db_name");

Link to comment
Share on other sites

I figured it out. The problem was with escapeshellarg on $db_name , it places single quotes around the variable so it was screwing up the execution of the page. I also switched the last three $result to their own functions, and used file_get_contents() for them instead of CURL. I only used curl on the first one. I don't think those were working in CURL because of the ampersands they contained. It works perfect now. I plugged this into the aMember signup page so when a user signs up a new database is created for them and when they loggin they are using their own database, it also uplaods the structure and default data to their new database.

 

Problem solved!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.