简体   繁体   English

通过cPanel用PHP创建mysql数据库并导入* .sql文件

[英]Create mysql DB with PHP through cPanel and import *.sql file

I want to create a DB for each user who wants to register to my Web-app. 我想为每个想要注册到我的Web应用程序的用户创建一个数据库。 When i try this local everything perfectly works. 当我尝试使用此本地工具时,一切正常。 A User can register with his email and pw. 用户可以用他的电子邮件和密码注册。 A DB is created and a import.sql file is imported so that the DB created for the User is empty on data but the tables and realtioships are given. 将创建一个数据库,并导入一个import.sql文件,以便为用户创建的数据库在数据上为空,但给出了表和域。

My Host uses cPanel and phpMyAdmin to create DB and db-user. 我的主机使用cPanel和phpMyAdmin创建数据库和数据库用户。 I want to do this with php like i do it local. 我想用php做到这一点,就像我在本地一样。 Does anyone have an idea how i get this done? 有谁知道我如何做到这一点?

I've already searched and tryed stuff out but nothing works for me. 我已经搜索并尝试了一些东西,但对我没有任何帮助。

If possible some explanations on cPanel maybe would be enough. 如果可能的话,对cPanel的一些解释可能就足够了。

            <?php
            require("xmlapi.php"); // this can be downlaoded from https://github.com/CpanelInc/xmlapi-php/blob/master/xmlapi.php
            $xmlapi = new xmlapi("cpanlel host name");   
            $xmlapi->set_port( 2083 );   
            $xmlapi->password_auth('cpanel username','cpanel passsword');    
            $xmlapi->set_debug(0);//output actions in the error log 1 for true and 0 false 

            $cpaneluser='cpanel username';
            $databasename="something";
            $databaseuser="database username";
            $databasepass= 'database password';

            //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('' . $databasename . '', '' . $databaseuser . '', 'all'));
            ?>

Using the above code you will be able to create new database, add new user to database and give all access to user on database. 使用上面的代码,您将能够创建新数据库,将新用户添加到数据库,并对数据库上的用户授予所有访问权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM