MySQL结果的多维数组

时间:2010-09-26 18:25:36

标签: php mysql

这是MySQL查询:

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

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

MySQL输出:
alt text

2 个答案:

答案 0 :(得分:2)

怎么样:

$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);

这将打印出来:

[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

等等。

答案 1 :(得分:1)

我对此进行了测试并且有效:

$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);

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