2

This is MySQL Query:

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

MySQL Output:
alt text

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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 Comment

Of course I don't have a schema named zfk so I used my test schema.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.