how to create mysql DB on shared hosting using php script

srsbiz

Registered
Jan 18, 2021
1
0
0
California
cPanel Access Level
Website Owner
I need to create a NEW MySQL database, NEW Table, and add users using php script.
I am using Go Daddy Shared Hosting with cPanel Deluxe..

I read that I will need to use UAPI, or XML API or something like that..

I have xmlapi.php in the same directory as my test php file.

I also read that Go Daddy Shared hosting does not allow this to work?

Can anyone help ?


<?php
include("xmlapi.php");

$db_host = 'website.com';
$cpaneluser = 'cpanelusername';
$cpanelpass = 'cpaneluserpassword';

$databasename = 'testdb';
$databaseuser = 'newdbuser'; // Warning: in most of cases this can't be longer than 8 characters
$databasepass = 'password'; // Warning: be sure the password is strong enough, else the CPanel will reject it

$xmlapi = new xmlapi($db_host);
$xmlapi->password_auth("".$cpaneluser."","".$cpanelpass."");
$xmlapi->set_port(2082);
$xmlapi->set_debug(1);//output actions in the error log 1 for true and 0 false
$xmlapi->set_output('array');//set this for browser output
//create database
$createdb = $xmlapi->api1_query($cpaneluser, "Mysql", "adddb", array($databasename));
//create user
$usr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduser", array($databaseuser, $databasepass));
//add user
$addusr = $xmlapi->api1_query($cpaneluser, "Mysql", "adduserdb", array("".$cpaneluser."_".$databasename."", "".$cpaneluser."_".$databaseuser."", 'all'));
?>
 
Last edited by a moderator:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
16,505
2,605
363
cPanel Access Level
Root Administrator
Hey there! We actually have an older thread with a good example of this work here, so I'd recommend checking that out to see if that helps your situation:


I'm not sure what restrictions GoDaddy may have on your account as that is something you'd need to speak with them about, but let me know if you need anything else!