How To Create a MySQL Database In cPanel With PHP

When you need to create a new database on a shared hosting account you normally need to log in to cPanel, create the database, create a user, and add the user to the database. That may not sound like a big deal, but it gets annoying fast when you need more than a couple of databases, or when you’re trying to find a way to quickly install some kind of a script or CMS (e.g. WordPress) on multiple domains.

There are various hacks & PHP scripts that automatically access cPanel and create a database – or so I’ve heard. I couldn’t find anything that would work on my host without modification. So I wrote my own basic cPanel class that can create MySQL databases and users (based in part on 5ubliminal’s script). You can use it as a building block for your own PHP scripts and tools.

Download The Script

cpanel.class.zip (8 Kb)

The archive contains :

  • includes/cpanel.class.php – the class itself. Feel free to extend or modify it. Links/credits are appreciated.
  • includes/ecurl.class.php – a cURL wrapper class. This is a modified version of another 5ubliminal’s script.
  • cpanel_createdb.php – a simple example that demonstrates how to use the class.

Notes

As you might have guessed, the script needs the cURL module to function. I haven’t tested it extensively; some small modifications might be necessary to make it work with cPanel skins other than x3. For example, the mysql_list_databases() function would probably need different regexps. Feel free to report any incompatibilities in the comments.

By the way, if you make a one-step WordPress using this, let me know – I was too lazy to program it myself 😉

Related posts :

32 Responses to “How To Create a MySQL Database In cPanel With PHP”

  1. White Shadow says:

    It could be.

    Anyway, what happens is this : the script uses CURL to access the cPanel pages. The problematic line is supposed to make the CURL functions to automatically follow HTTP redirects. You can comment out the line and, assuming that the script doesn’t encounter any redirects while doing whatever you want it to do, it should work fine.

  2. Tony Chambers says:

    Whilst trying this straight out of the box (after editing my username etc) my webpage will not finish loading.

    Any ideas?

  3. Mike Solstice says:

    THANK YOU THANK YOU THANK YOU!! omg. 2 Weeks of Googling. Posted on 50+ PHP forums. Everywhere I went I get “that’s not possible” (create a db w/o logging into cpanel) but I’ve seen other scripts do it, so I knew it was possible & it is an absolutely essential process to my site as I don’t allow my clients cPanel access to begin with.

    For the record, works “out of the box” on HostGator Reseller. Plugged in a username & password & presto. I about flew out of my chair lol Thank you!!!

  4. Indunil says:

    THANK YOU THANK YOU THANK YOU

    THANK YOU THANK YOU THANK YOU

    THANK YOU THANK YOU THANK YOU

    THANK YOU THANK YOU THANK YOU

    THANK YOU THANK YOU THANK YOU

    THANK YOU THANK YOU THANK YOU

    THANK YOU THANK YOU THANK YOU

    THANK YOU THANK YOU THANK YOU

    THANK YOU THANK YOU THANK YOU

    THANK YOU THANK YOU THANK YOU

    THANK YOU THANK YOU THANK YOU

    THANK YOU THANK YOU THANK YOU

  5. Hi, just wanted to tell you, I loved this blog post. It was helpful.
    Keep on posting!

  6. Mohammad Shaikh says:

    Can anyone detail the install directions. I can’t get it to work on my end.

  7. Asep Indra K says:

    Hey, this is amazing source code, THANK YOU VERY MUCH 🙂

  8. Yogesh Patil says:

    I am facing one doubt , i am confused about “$cpanel_skin” vaiable. What it is and how to find value in Godaddy cpanel.

    Thank s in advance.

  9. Jānis Elsts says:

    It’s the name of the cPanel theme or skin. It’s usually part of the URL, like in “…/frontend/skin-name/index.html”.

  10. Samuel says:

    Its a good basic cPanel class that can create MySQL databases.
    Keep up the good work.

  11. Ashwini says:

    Hello,
    am not able to create db in cpanel using php 401 error occuring even am providing correct details.

  12. Jānis Elsts says:

    Unfortunately, the script is quite old and might not work with the latest version of cPanel.

Leave a Reply