How to create database programmatically in WHM/Cpanel using php code?

660 views Asked by At

I had xmlapi code before 1.5 years ago and working perfectly till 6 months before. Suddenly, it stopped working with no error. Now, I read that xmlapi has been deprecated and uapi is being used. Now, I prepare code as per - cpanel-UAPI-php-class with no error but both xmlapi and uapi could not create database.

Is it possible to make database dynamic as we do in local machine(using query)? Or any other way to work permanently.

1

There are 1 answers

0
Peter B On

You can use cPanels LiveAPI class to achieve this:

<?php

// Instantiate the CPANEL object.
require_once "/usr/local/cpanel/php/cpanel.php";

$cpanel = new CPANEL(); // Connect to cPanel - only do this once.
  
// Create a new database.
$create_db = $cpanel->uapi(
    'Mysql', 'create_database',
    [
        'name'    => 'newdb',
    ]
);

Don't forget that if database prefixing is enabled, newdb will need to also include the username. For example, cpuser_newdb.

More info:

https://documentation.cpanel.net/display/DD/UAPI+Functions+-+Mysql%3A%3Acreate_database https://documentation.cpanel.net/display/DD/Guide+to+the+LiveAPI+System+-+PHP+Class