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 Rohan Purekar, May 31, 2015.

  1. Rohan Purekar

    Rohan Purekar Registered

    Joined:
    May 31, 2015
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Nagpur
    cPanel Access Level:
    Website Owner
    Warning: Illegal string offset 'result'
    Warning: Invalid argument supplied for foreach() i am getting the following errors

    here's the code of tomdchi which i used
    Code:
    include("xmlapi.php"); 
    $db_host = "mydomain.com"; 
    $cpuser = "cpusername"; 
    $databasename = 'wxa';//do not prepend with username
    $databaseuser = 'mynewuser';//api will do that for you
    $databasepass = 'pass';
    
    $xmlapi = new xmlapi($db_host); 
    $xmlapi->password_auth("cpanelusername","cpanelpassword"); 
    $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);
    
    
     
    #1 Rohan Purekar, May 31, 2015
    Last edited by a moderator: May 31, 2015
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    651
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello,

    I moved this post into it's own thread, as the example and thread you are using is over five years old. I suggest using cPanel API 2. Documentation on this module is available at:

    cPanel API 2 Functions - MysqlFE::createdb

    Thank you.
     
  3. tecwithquestion

    tecwithquestion Active Member

    Joined:
    Oct 20, 2014
    Messages:
    38
    Likes Received:
    0
    Trophy Points:
    6
    cPanel Access Level:
    Root Administrator
    Try this one
    <?php
    include("xmlapi.php");
    $db_host = "localhost";
    $cpuser = "user";
    $databasename = 'dbb';//do not prepend with username
    $databaseuser = 'dbb';//api will do that for you
    $databasepass = 'P3cJdlvx/vAo5cSRR5djAA==';

    $xmlapi = new xmlapi($db_host);
    $xmlapi->password_auth("root","rootpass");
    $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);


    ?>
     
Loading...

Share This Page