Php 从MySQL数据库创建嵌套数组
我有一个包含一些表和列的数据库。 我想在PHP中创建一个多维数组来存储这些值Php 从MySQL数据库创建嵌套数组,php,mysql,multidimensional-array,Php,Mysql,Multidimensional Array,我有一个包含一些表和列的数据库。 我想在PHP中创建一个多维数组来存储这些值 $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_fetc
$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个小时在键盘上敲击我的头,谷歌试图找出答案。。。有很多关于将多维数组放入SQL的教程,但关于其他方面的教程不多
谢谢
$tableData = array(array());
...
array_push($tableData, $row[0]);
...
这只意味着您将得到一个如下所示的数组:
array(array(), 'foo', 'bar', 'baz')
$tableData = array();
...
array_push($tableData, array($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;
}
}
祝你好运,多尝试一下这个。。。
是您的朋友。您对列名的查询不正确。您应该从mydb中显示mytable中的列;或显示mydb.mytable中的列 资料来源: 您可能希望执行以下操作:
$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];
}
}
虽然我还没有机会运行这段代码来查找错误
$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);
}
}
应该做到这一点。设置多维数组不需要arrayarray。只需将其他数组作为值推送即可 试试这个:
foreach ($tableNames as $tableName)
{
$result = mysql_query("SHOW COLUMNS FROM $tableName");
while ($row = mysql_fetch_assoc($result))
{
$tableData[$tableName] = $row;
}
}
最好直接寻址信息模式,并在此基础上创建数组:
// 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' ];
}
所有的答案似乎都是正确的,但这是最简单的。我使用了一种变体,使用mysql_QueryDescripte$tableName和$row['Field']代替$tableName和$row中的显示列。