即时编辑活动数据库 [英] Edit active database on the fly

查看:165
本文介绍了即时编辑活动数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在我可以使用以下函数来检索所有的数据库。

  DB :: select('SHOW DATABASES')

但是,我现在想要能够获取每个数据库的表,并最终对这些数据库做更多的工作,但是我想我是否可以使这个工作不成问题。



通常你的配置中有不同的数据库,但是由于我希望我的应用程序使用任何数据库,并且确保我不必手动添加所有的数据库等,因为那是我想要我的网络应用程序为我完成的工作。



我已经尝试欺骗了一点没有成功例如。 p>

  DB:select('USE dbName; SHOW TABLES'); 
DB :: select('SELECT dbName(); SHOW TABLES');

显然这没有办法,但是是否有适当的解决方案?我认为编辑.env变量可能是一个选项,但我似乎找不到一个合法的方式来做到这一点。

解决方案

你不需要这个。


我以为可以编辑.env变量我已经是一个
的选项,但我似乎找不到一个合法的方式来做到这一点。


你需要的是这个

  DB :: purge('mysql'); // IMP 

Config :: set('database.connections.mysql.host',$ host);
Config :: set('database.connections.mysql.database',$ database);
Config :: set('database.connections.mysql.username',$ username);
Config :: set('database.connections.mysql.password',$ password);

您只需找出一种获取 $ host的值的方法 $ database $ username $ password 动态。



一种方法是存储所有这些值并指向默认数据库连接的数据库(例如 config / database.php 中的mysql )。然后从中读取值,并相应地设置连接。


So I'm working on a little web application in which you can manage your database.

Now I can use the following function to retrieve all databases

DB::select('SHOW DATABASES')

But I now want to be able to get the tables for each of those databases and eventually do more with those databases, but I figured if I get this working that wouldn't be a problem.

Normally you'd have the different database in your config, but since I want my application work with "any" database and make sure I don't have to manually add all the databases etc since that's the kind of work I want my web app done for me.

I've tried tricking around it a bit without success for example.

DB:select('USE dbName; SHOW TABLES');
DB::select('SELECT dbName(); SHOW TABLES'); 

Obviously this didn't work, but is there any "proper" solution to this? I thought editing the .env variable on the fly might've been an option, but I can't seem to find a "legit" way to do that either.

解决方案

You don't need this.

I thought editing the .env variable on the fly might've been an option, but I can't seem to find a "legit" way to do that either.

What you need is this

DB::purge('mysql');//IMP

Config::set('database.connections.mysql.host', $host);
Config::set('database.connections.mysql.database', $database);
Config::set('database.connections.mysql.username', $username);
Config::set('database.connections.mysql.password', $password);

You just need to figure out a way to get the values for $host, $database, $username, and $password dynamically.

One way to do that is have a database which stores all these values and point the default database connection (say mysql in config/database.php) to it. Then read values from it on the fly and set the connections accordingly.

这篇关于即时编辑活动数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆