简体   繁体   English

将变量数据从PHP传递到shell_exec命令

[英]Pass variable data from PHP to shell_exec command

I have a script that clones tables on to uniquely named MySQL databases from a master sql dump file . 我有一个脚本,可以从主sql转储文件中将表克隆到唯一命名的MySQL数据库上。 Each account has their own database, but the table structure is the same for all accounts. 每个帐户都有自己的数据库,但是所有帐户的表结构都相同。 My solution was to dump the master database table and then through PHP shell_exec, run the MySQL controller cmd (mysql) to populate the newly created database with default tables. 我的解决方案是转储主数据库表,然后通过PHP shell_exec,运行MySQL控制器cmd(mysql),以默认表填充新创建的数据库。

At Issue: The process works but only when I hard code the accounts unique database name in the master sql dump file. 出现问题时:该过程有效,但仅当我在主sql转储文件中硬编码帐户唯一数据库名称时。

However, "USE acct_dbID" line inside the master sql file needs to be dynamically set at runtime. 但是,需要在运行时动态设置主sql文件中的“ USE acct_dbID”行。

Here is the code: 这是代码:

include('.dbase_credentials'); //constants for connection object

//using PHP built in connection class mysqli
$mysqli = new mysqli(DB_HOST,DB_UNAME,DB_UPWORD,DB_NAME);
if ($mysqli->connect_errno){
    echo "Failed to open database connection: ".$mysqli->connect_error;
    exit();
}

$dbID=$varNum; //variable, number ID generated earlier in the account setup process

//create database, doesnt return a resultset, no need for object var here
if ($mysqli->query("CREATE DATABASE acct_".$dbID) === TRUE){

    //if dbase was created, clone the tables
    $res = shell_exec('mysql -u dbaseUser --password=`cat /path/to/pass` -e "source /path/to/master_tables.sql" acct_'.$dbID);

    //provide some UI feedback, shell_exec returns null on failure
    if ($res!=null){
        echo "The tables were cloned!";
    }else{
        echo "The cloning process failed!";
    }
}else{
    echo "no database created.";
}

So again, master_tables.sql needs variable data passed it at runtime so the "USE acct_dbID" can be specific to each new account. 同样,master_tables.sql需要在运行时传递变量数据,以便“ USE acct_dbID”可以特定于每个新帐户。

Any thoughts are appreciated. 任何想法表示赞赏。 rwhite35 rwhite35

knittl had the right direction for what I need to do. knittl为我需要做的事情指明了正确的方向。 See the comment conversation above. 请参阅上面的评论对话。 Thanks knittl. 谢谢knittl。 Here is the new code block using multi_query and file_get_contents. 这是使用multi_query和file_get_contents的新代码块。

if ($mysqli->query("CREATE DATABASE acct_".$dbID) === TRUE){
$acct="acct_".$dbID;
$query = "USE ".$acct.";";
$query .= file_get_contents('/path/to/master_table.sql');
$result=$mysqli->multi_query($query);           
}

Now account can be variable and multi_query will run each query statement in the string. 现在account可以是变量,并且multi_query将运行字符串中的每个查询语句。 I probably lose some efficiency but gain the ability to clone tables for each new account. 我可能会失去一些效率,但是获得了为每个新帐户克隆表的功能。 The only additional edit made was to the master_table.sql. 唯一进行的其他编辑是对master_table.sql的修改。 I removed all the comments that the dump process adds. 我删除了转储过程中添加的所有注释。 Thanks, rwhite35 谢谢,rwhite35

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

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