assigning new database to user account using php?

-Roger-

Member
Mar 16, 2011
5
0
51
Hi,
I have written php code that will create a new cpanel account, create a MySQL database for it and assign a username and password for the database, but I can't figure out how to associate the mysql database with the cpanel useraccount I created.

The database uses the same username as the cpanel account, it just doesnt show up when I login to the cpanel for that account and go to the MySQL databases page. However, the database does show up fine in phpMyAdmin, and I can add tables to it etc.

So, does anyone know what I'm missing? I assume I have to edit the user account to tell it it has a database to hold hands with... but how?

This is all so I can set up accounts with databases remotely from a webpage without having to go through WHM and cpanel.

Thanks,
Roger
 

cPanelDavidN

Well-Known Member
Staff member
Dec 17, 2009
571
3
68
Houston, TX
cPanel Access Level
Root Administrator
Hi Roger,

Did you happen to read this blog post about using the XML-API PHP client class. The context of the article is for a postwwwacct script hook, but the information regarding the creation of databases still holds true to any context of making an xml-api query to perform database operations using API1's Mysql module.

The database uses the same username as the cpanel account, it just doesnt show up when I login to the cpanel for that account and go to the MySQL databases page. However, the database does show up fine in phpMyAdmin, and I can add tables to it etc.
When you say you use phpMyAdmin, are you saying that you login into the cPanel UI with the user's credentials and open phpMyAdmin and it's listed there? Or are you logging into phpMyAdmin as a privileged user, aka root, via WHM? Where you see it listed in phpMyAdmin, is the database prefixed with the user's name?

Regards,
-DavidN
 
Last edited:

-Roger-

Member
Mar 16, 2011
5
0
51
Hi,
I login via root access and yes the database is prefixed with the account username, but that's because that's what my script named it when it created it.
I will read that blog, but I did try the XML API for PHP, but it required php5 and we have 4.something on this server.
Thanks,
Roger
 

-Roger-

Member
Mar 16, 2011
5
0
51
Hi,
I read the blog, and kind of followed it, but I can't use the xmlapi.php class, because it always just returns an error:

Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/mediatwo/public_html/src/xmlapi.php on line 103

It's probably meant for php5, which we can't upgrade to because too many sites on the server will break if we do.

I guess I'll just have to live with the accounts not knowing there is a database with the same name. Bit annoying when removing accounts though, because then I'll have to go and remove the database it used manually.
 

-Roger-

Member
Mar 16, 2011
5
0
51
I suspect the code I need to tell an account that it has a database connected to it is in the xmlapi.php file, but I wouldn't have a clue which bit! lol
 

cPanelDavidN

Well-Known Member
Staff member
Dec 17, 2009
571
3
68
Houston, TX
cPanel Access Level
Root Administrator
Hi Roger,

Yes, the XML-API PHP client class was written for PHP 5. I suppose it's possible for someone to rewrite it for compatibility with PHP 4. Really the meat of the class is in the "xmlapi_query", "api1_query", "api2_query" and "curl_query" & "fopen_query" methods. Additionally you'll need some way to read the response (either XML or JSON). IIRC, PHP 4 never shipped with SimpleXML, which is what the class uses. There are a handful of descent pure-PHP XML parsers out there though.

As far as the Parse error, I can only imagine that there's some PHP 5 object syntax that is causing the PHP 4 parser to freak out, like visibility

I guess I'll just have to live with the accounts not knowing there is a database with the same name.
So, you saying that for an account with a username "john" your're adding a database named "john_john"? If this is the case, I would reconsider. There are some unique things that could make such a request not work. I can give a more detailed answer if you need, but suffice it to say that cPanel's validation has not always supported that (when manually creating the databases) and changes since 11.26 have made it possible to have a database as such, but not by creation via XML-API.

What version of cPanel are you running? This may have a combined affect the paragraph above.

I strongly encourage you to do what it takes to get those sites off PHP 4. There have been countless bug fixes and, more importantly, security fixes to the PHP core. Most code that runs under PHP 4 can run with little or no change on PHP 5. But that's only a suggestion, I can't speak to you business needs:)

Regards,
-DavidN
 

-Roger-

Member
Mar 16, 2011
5
0
51
Hi,
Thanks for your answer. To clarify, no I'm not naming the databae the same as the useraccount. They are like john_cms and the account name is john. I would have thought that the matching account and database prefix would pair together for cpanel, but nope, when I check in cpanel/mysql databases, it says there are no dbs for that account, which means when removing the account, it will leave the db behind. That's my only real issue, having to manually remove databases that have been created with my script.

WHM version is WHM 11.28.86
cPanel version is 11.28.87

Our head php programmer tells me that too many sites would just break if the server was upgraded to php5. We do have a couple of other dedicated servers, and one of them is running php5.
 

cPanelDavidN

Well-Known Member
Staff member
Dec 17, 2009
571
3
68
Houston, TX
cPanel Access Level
Root Administrator
Hi Roger,

That's very odd indeed. There's no reason that some can create a database for "john" called "john_cms" using the APIs and not have it appear in the cPanel interface. (unless something is wrong).

I just sent you a PM; I think this issue might warrant a look-over by the Tech Support and Integration Development teams.

Regards,
-DavidN