The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

create database without login to cpanel

Discussion in 'cPanel Developers' started by ibnuyahya, Sep 10, 2009.

  1. ibnuyahya

    ibnuyahya Registered

    Joined:
    Sep 9, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Hi,

    I'm looking for a solution to create database without login to cpanel. I impress with this software ( expertwp.s3.amazonaws.com/viral/EWP-Demo1-Flash/EWP-Demo1-Flash.html ) . How can I do that with only have a normal user right?... I mean any cpanel shared hosting user can do that. Is there any API and sample in php?
     
    #1 ibnuyahya, Sep 10, 2009
    Last edited: Sep 17, 2009
  2. ibnuyahya

    ibnuyahya Registered

    Joined:
    Sep 9, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Ok I got it. I'm using library created by Matt

    PHP:
    include("xmlapi.php.inc");
    $db_host "myDomainName.com";
    $cpuser "myCpanelUser";
    $cppass "myCpanelPwd";

    $xmlapi = new xmlapi($db_host);
    $xmlapi->set_port(2083);
    $xmlapi->password_auth($cpuser,$cppass);
    $xmlapi->set_debug(1);
    //create database
    print $xmlapi->api1_query($cpuser"Mysql""adddb"'myDatabaseName');
    //create user
    print $xmlapi->api1_query($cpuser"Mysql""adduser", array('user' => 'myDBUser','pass'=>'myDBPwd'));
    It work fine.. but can anyone help me how to add myDBUser to myDatabaseName with privilage?...

    I have read this post but not really sure how to use this
    <cpanel Mysql="adduserdb($FORM{'db'},$FORM{'user'},$FORM{'ALTER'} $FORM{'CREATEROUTINE'} $FORM{'CREATETEMPORARYTABLES'} $FORM{'CREATE'} $FORM{'DELETE'} $FORM{'DROP'} $FORM{'SELECT'} $FORM{'INSERT'} $FORM{'UPDATE'} $FORM{'REFERENCES'} $FORM{'INDEX'} $FORM{'LOCKTABLES'} $FORM{'ALL'})">
     
    #2 ibnuyahya, Sep 10, 2009
    Last edited: Sep 17, 2009
  3. ibnuyahya

    ibnuyahya Registered

    Joined:
    Sep 9, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Finally, I understand how to use it... :D

    PHP:
    include("xmlapi.php.inc");
    $db_host "myDomainName.com";
    $cpuser "myCpanelUser";
    $cppass "myCpanelPwd";

    $xmlapi = new xmlapi($db_host);
    $xmlapi->set_port(2083);
    $xmlapi->password_auth($cpuser,$cppass);
    $xmlapi->set_debug(1);
    //create database
    print $xmlapi->api1_query($cpuser"Mysql""adddb"'myDatabaseName');
    //create user
    print $xmlapi->api1_query($cpuser"Mysql""adduser", array('user' => 'myDBUser','pass'=>'myDBPwd'));
    //add user to database
    $xmlapi->api1_query($cpuser"Mysql""adduserdb", array(
                        
    'db' => 'myDatabaseName',
                        
    'user'=>'myDBUser',
                        
    'SELECT INSERT UPDATE CREATE DELETE ALTER DROP'));

    Problem solved
     
    #3 ibnuyahya, Sep 11, 2009
    Last edited: Sep 17, 2009
  4. battisti

    battisti Member

    Joined:
    Nov 7, 2009
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    1
    another soluction

    you can do that

    PHP:
    include("xmlapi.php.inc"); 
    $db_host "myDomainName.com"
    $cpuser "myCpanelUser"
    $cppass "myCpanelPwd"

    $xmlapi = new xmlapi($db_host); 
    $xmlapi->set_port(2083); 
    $xmlapi->password_auth($cpuser,$cppass); 
    $xmlapi->set_debug(1); 
    //create database 
    print $xmlapi->api1_query($cpuser"Mysql""adddb"'myDatabaseName'); 
    //create user 
    print $xmlapi->api1_query($cpuser"Mysql""adduser", array('myDBUser','myDBPwd')); 
    //add user to database 
    $xmlapi->api1_query($cpuser"Mysql""adduserdb", array( 
                        
    'myDatabaseName'
                        
    'myDBUser'
                        
    'all')); 
    you can pass a simple array not a hash
     
  5. tomdchi

    tomdchi Well-Known Member

    Joined:
    Feb 24, 2008
    Messages:
    116
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Atlanta, GA
    cPanel Access Level:
    DataCenter Provider
    To get this to work I had to change a couple of things. The first is that in the examples above it shows

    Code:
    $cpuser = "myCpanelUser";  
    $cppass = "myCpanelPwd"; 
    
    $xmlapi->password_auth($cpuser,$cppass);  
    
    In my setup I used

    Code:
    $cpuser = "myCpanelUser";  
    $cppass = "myCpanelPwd"; 
    
    $xmlapi->password_auth('root','root_password');  
    
    I don't think that you can use the cpanel accounts user and password as authorization for the api.

    I had to take out this line
    Code:
    $xmlapi->set_port(2083); 
    

    Full code I used. One thing to remember. Per the api docs $cpuser is the cpanel account username that you are performing operations on.

    Code:
    
    <?php  
    
    include("xmlapi.php");  
    $db_host = "localhost";  
    $cpuser = "myuser";  
    $databasename = 'mydatabasename';//do not prepend with username
    $databaseuser = 'mydatabaseuser';//api will do that for you
    $databasepass = '123456';
    
    $xmlapi = new xmlapi($db_host);  
    $xmlapi->password_auth("root","root_pass");  
    $xmlapi->set_debug(1);//this setting will put output into the error log in the directory that you are calling script from 
    $xmlapi->set_output('array');//set this for browser output
    //create database  
    $createdb = $xmlapi->api1_query($cpuser, "Mysql", "adddb", array($databasename)); 
    foreach($createdb as $v)
    {
        $result = $v['result'];
    }
    if ($result == 1)
    {
        //create user  
        $usr = $xmlapi->api1_query($cpuser, "Mysql", "adduser", array($databaseuser, $databasepass));  
    }
    foreach($usr as $v)
    {
        $result2 = $v['result'];
    }
    if ($result2 == 1)
    {
        //add user to database  
        $addusr = $xmlapi->api1_query($cpuser, "Mysql", "adduserdb", array($databasename, $databaseuser, 'all'));  
    
    }
    print_r($addusr);
    
    
    ?>
    
    
    Hope this helps anyone looking to set up a database through the api.

    Tom
     
  6. wanderingstan

    wanderingstan Registered

    Joined:
    Jul 13, 2011
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Don't know the cause, but I had to but the database name in an array for this to work. E.g.
    Code:
    //create database 
    print $xmlapi->api1_query($cpuser, "Mysql", "adddb", [B]array('myDatabaseName')[/B]);  
    I was tipped to this idea by the code in this thread.
     
Loading...

Share This Page