2011-08-24 16 views
0

私はいくつかのテーブルとカラムを持つデータベースを持っています。 これらの値を格納するためにPHPで多次元配列を作成したいとします。MySQLデータベースからネストされた配列を作成する

$tableData = array(array()); // I try to set up the multidimensional array. 

/* 1: Get Table Names (works fine) */ 
    $result = mysql_query("SHOW TABLES FROM myDatabase"); 
    $i=0; 
    while ($row = mysql_fetch_row($result)) 
    {     
     // Push the table name into the PHP array 
     array_push($tableData, $row[0]); 
    } 


/* 2: Columns */ 
    for ($i = 1; $i < count($tableNames); $i++) 
    { 
     $result = mysql_query("SHOW COLUMNS FROM $tableData[$i]");   
     while ($row = mysql_fetch_assoc($result)) 
     { 
      array_push($tableData[$i], $row); // ** ERROR THROWN: first argument must be an array ** // 
     } 
    } 

どうすればいいですか?私はこれがシンプルでなければならないと感じていますが、私は最後の4時間を私のキーボードとGoogleで頭を叩きながら試してみました。多次元配列をSQLに入れるのに多くのチュートリアルはありますが、他の方法で周り!

ありがとうございます!

答えて

0

多次元配列を設定するために配列(array())を行う必要はありません。他の配列を値としてプッシュするだけです。

はこれを試してみてください。

+0

すべての答えが正しいように見えるが、この1つは最も単純です。 "SHOW COLUMNS FROM $ tableName"と$ rowの代わりにmysql_query( "DESCRIBE $ tableName")と$ row ['Field']を使ってバリエーションを使用しています。 – zannorman

0
$tableData = array(array()); 
... 
    array_push($tableData, $row[0]); 
... 

これはちょうどあなたがこのようになります配列で終わる意味:

array(array(), 'foo', 'bar', 'baz') 

あなたがそのような「多次元配列を設定」することはできません。これは、より次のようになります。これにより

$tableData = array(); 
... 
    array_push($tableData, array($row[0])); 
... 

は、次のような配列で終わるだろう:

array(array('foo'), array('bar'), array('baz')) 

私はあなたがこのかかわらずの線に沿ってより多くの何かをしたいと思います

array('foo' => array('col1', 'col2', ...), 'bar' => array('col1', ...)) 

ですから、列に対して、このような何かを実行する必要があります。

$result = array(); 
foreach ($tableNames as $tableName) { // much more useful than for() 
    $result = mysql_query("SHOW COLUMNS FROM $tableName"); 
    while ($row = mysql_fetch_assoc($result)) { 
     $result[$tableName][] = $row; 
    } 
} 

これでもっと実験するのは幸運です...
var_dumpはあなたの友人です。

0

列名のクエリが正しくありません。あなたはSHOW COLUMNS FROM mytable FROM mydb;またはSHOW COLUMNS FROM mydb.mytable;

SOURCEような何かを行う必要があります:私はエラーのため、このコードを実行する機会がなかったが、

$tableData = array(); 

$result = mysql_query("SHOW TABLES FROM myDatabase"); 
while ($row = mysql_fetch_row($result)) 
{ 
    $tableData[$row[0]] = array(); 
} 

foreach ($tableData as $table => $array) 
{ 
    $result = mysql_query("SHOW COLUMNS FROM $table FROM myDatabase");   
    while ($row = mysql_fetch_assoc($result)) 
    { 
     $tableData[$table][] = $row[0]; 
    } 
} 

http://dev.mysql.com/doc/refman/5.0/en/show-columns.html

は、おそらくのような何かをしたいです。

0
$tableData = array(); 

/* 1: Get Table Names (works fine) */ 
    $result = mysql_query("SHOW TABLES FROM myDatabase"); 
    $i=0; 
    $tablenames = array(); 
    while ($row = mysql_fetch_row($result)) 
    {     
     // Push the table name into the PHP array 
     array_push($tablenames, $row[0]); 
    } 


/* 2: Columns */ 
    foreach($tablenames as $table) 
    { 
     $tableData[$table] = array(); 
     $result = mysql_query("SHOW COLUMNS FROM $table"); 
     while ($row = mysql_fetch_assoc($result)) 
     { 
      array_push($tableData[$table], $row); 
     } 
    } 

このトリックを行う必要があります。

0

あなたが直接INFORMATION_SCHEMAに対処し、それに基づいてアレイを作成する方がいいでしょう:

// table_schema is the database name. So you're looking up all of the tables 
// and their respective columns. 
$q = mysql_query('SELECT TABLE_NAME, COLUMN_NAME FROM COLUMNS WHERE '. 
        "TABLE_SCHEMA = 'myDatabase'"); 

$output = array(); 
while($r = mysql_fetch_assoc($q)) 
{ 
    // does an index exist for this table? 
    if(!isset($output[ $r[ 'table_name' ] ])) 
     // no? create one 
     $output[ $r[ 'table_name' ] ] = array(); 
    // append the array which exists at this index with the column name. 
    $output[ $r[ 'table_name' ] ][] = $r[ 'column_name' ]; 
} 
関連する問題