从 MySQL 数据库创建嵌套数组

发布于 2024-12-01 03:20:05 字数 860 浏览 0 评论 0原文

我有一个包含一些表和列的数据库。 我想在 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_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 的教程,但对于反过来说!

谢谢!

I have a database with some tables and columns.
I want to create a multidimensional array in PHP to store these values.

$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 ** //
        }
    }

How can I do this? I feel like this should be simple, but I've spent the last 4 hours banging my head on my keyboard and google trying to figure it out... plenty of tutorial on putting a multidimensional array into a SQL, but not much for the other way around!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

五里雾 2024-12-08 03:20:05

您不需要 array(array()) 来设置多维数组。只需将其他数组作为值推送即可。

试试这个:

foreach ($tableNames as $tableName) 
{
    $result = mysql_query("SHOW COLUMNS FROM $tableName");
    while ($row = mysql_fetch_assoc($result))
    {
        $tableData[$tableName] = $row;
    }
}

you don't need to array(array()) to set up a multidimensional array. just push other array as a value.

try this:

foreach ($tableNames as $tableName) 
{
    $result = mysql_query("SHOW COLUMNS FROM $tableName");
    while ($row = mysql_fetch_assoc($result))
    {
        $tableData[$tableName] = $row;
    }
}
楠木可依 2024-12-08 03:20:05
$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 是你的朋友。

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

This just means you end up with an array that looks like this:

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

You can't "set up a multi-dimensional array" like that. It should look more like this:

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

With this, you'd end up with an array like:

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

I guess you want more something along the lines of this though:

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

So you need to do something like this for the columns:

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

Good luck in experimenting more with this...
var_dump is your friend.

心不设防 2024-12-08 03:20:05
$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);
        }
    }

应该可以解决问题。

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

should do the trick.

彼岸花似海 2024-12-08 03:20:05

您对列名称的查询不正确。您应该执行类似 SHOW COLUMNS FROM mytable FROM mydb;SHOW COLUMNS FROM mydb.mytable;

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

你可能想做类似的事情:

$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];
    }
}

虽然我没有机会运行此代码以查找错误。

Your query for column names isn't correct. You should do something like SHOW COLUMNS FROM mytable FROM mydb; or SHOW COLUMNS FROM mydb.mytable;

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

You probably want to do something like:

$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];
    }
}

Although I haven't had a chance to run this code for errors.

甚是思念 2024-12-08 03:20:05

您最好直接处理 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' ];
}

You're better off addressing the INFORMATION_SCHEMA directly and creating arrays based on that:

// 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' ];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文