首页 > 解决方案 > 循环访问名称存储在主数据库中的数据库

问题描述

所以我有一个主数据库,其中存储了其他数据库的名称。我需要以某种连接方式循环存储在主数据库中的所有数据库的槽表。status != 2我需要站点数据库中使用 PHP 和 MySQLi的所有行的数组。

主要的看起来像这样:

main.stations

+----+-----------------+------+
| ID |  station name   |  db  |
+----+-----------------+------+
|  1 | Name of station | db_1 |
|  2 | Name again      | db_2 |
|  3 | Name            | db_3 |
+----+-----------------+------+

所有三个站都有一个指定的数据库,其名称在列中说明main.stations.db

该站的数据库有两个表,我需要循环遍历并获取所有行status != 2。这两个表如下所示(以 db_1 为例):

db_1.salesReport

+----+----------+--------+
| ID |   name   | status |
+----+----------+--------+
|  1 | Report 1 |      1 |
|  2 | Report 2 |      0 |
|  3 | Report 3 |      2 |
+----+----------+--------+

db_1.fuelReport

+----+----------+--------+
| ID |   name   | status |
+----+----------+--------+
|  1 | Report 1 |      1 |
|  2 | Report 2 |      0 |
|  3 | Report 3 |      2 |
+----+----------+--------+

我的问题是;有没有办法根据来自另一个数据库的数据库名称循环访问数据库?(当然假设用户有权访问所有数据库)我不想在基于站点的阵列中添加另一层。我希望子数据库中的每一行都是我的数组中的顶级元素,包括来自车站的所有信息。

编辑 输出必须看起来像这样

0:{
stationID: 1
station name: Name of stations,
fromDb: salesReport //this doesn't have to be dynamically stated
name: Report 1,
status: 1
},
1:{
stationID: 1
station name: Name of stations,
fromDb: fuelReport
name: Report 1,
status: 1
},
2:{
stationID: 1
station name: Name of stations,
fromDb: salesReport
name: Report 2,
status: 0
},
3:{
stationID: 2
station name: Name again,
fromDb: salesReport
name: Report 2,
status: 0
},

等等..

标签: phpmysqlsqlmysqli

解决方案


推荐阅读