MySQL结果的多维数组

时间:2021-10-19 02:12:06

This is MySQL Query:

这是MySQL查询:

SELECT `TABLE_SCHEMA` , `TABLE_NAME` , `COLUMN_NAME`
FROM `COLUMNS` WHERE `TABLE_SCHEMA` = 'zfk'

How can I make multi dimensional arrays:
Level 1 TABLE_SCHEMA (The above query is just a demo, else real query has no where clause).
Level 2 TABLE_NAME
Level 3 COLUMN_NAME

如何创建多维数组:Level 1 TABLE_SCHEMA(上面的查询只是一个演示,否则真正的查询没有where子句)。 Level 2 TABLE_NAME等级3 COLUMN_NAME

MySQL Output:
MySQL结果的多维数组

2 个解决方案

#1


2  

How about:

$query = mysql_query("SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME
FROM COLUMNS WHERE TABLE_SCHEMA = 'zfk'");

$resulting_array = array();
while($row = mysql_fetch_array($query, MYSQL_NUM)){
    $resulting_array[] = $row;
}

print_r($resulting_array);

This will print out:

这将打印出来:

[0] => array([0] => zfk, [1] => zfk_clients, [2] => id
[1] => array([0] => zfk, [1] => zfk_clients, [2] => company_name
[2] => array([0] => zfk, [1] => zfk_clients, [2] => web_address

and so on.

等等。

#2


1  

I tested this and it works:

我测试了它,它的工作原理:

$pdo = new PDO("mysql:dbname=information_schema", "XXXX", "XXXX");
$sql = "SELECT `TABLE_SCHEMA` , `TABLE_NAME` , `COLUMN_NAME`
        FROM `COLUMNS` WHERE `TABLE_SCHEMA` = 'zfk'";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$zfk = array();
while ($row = $stmt->fetch()) {
  $s = $row["TABLE_SCHEMA"];
  $t = $row["TABLE_NAME"];
  $c = $row["COLUMN_NAME"];
  $zfk[$s][$t][$c] = 1;
}
print_r($zfk);

It outputs a three-dimensional array with schema names as the first dimension, table names as the second dimension, and column names as the third dimension.

它输出一个三维数组,其中模式名称作为第一维度,表名称作为第二维度,列名称作为第三维度。

#1


2  

How about:

$query = mysql_query("SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME
FROM COLUMNS WHERE TABLE_SCHEMA = 'zfk'");

$resulting_array = array();
while($row = mysql_fetch_array($query, MYSQL_NUM)){
    $resulting_array[] = $row;
}

print_r($resulting_array);

This will print out:

这将打印出来:

[0] => array([0] => zfk, [1] => zfk_clients, [2] => id
[1] => array([0] => zfk, [1] => zfk_clients, [2] => company_name
[2] => array([0] => zfk, [1] => zfk_clients, [2] => web_address

and so on.

等等。

#2


1  

I tested this and it works:

我测试了它,它的工作原理:

$pdo = new PDO("mysql:dbname=information_schema", "XXXX", "XXXX");
$sql = "SELECT `TABLE_SCHEMA` , `TABLE_NAME` , `COLUMN_NAME`
        FROM `COLUMNS` WHERE `TABLE_SCHEMA` = 'zfk'";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$zfk = array();
while ($row = $stmt->fetch()) {
  $s = $row["TABLE_SCHEMA"];
  $t = $row["TABLE_NAME"];
  $c = $row["COLUMN_NAME"];
  $zfk[$s][$t][$c] = 1;
}
print_r($zfk);

It outputs a three-dimensional array with schema names as the first dimension, table names as the second dimension, and column names as the third dimension.

它输出一个三维数组,其中模式名称作为第一维度,表名称作为第二维度,列名称作为第三维度。