从 MySQL 数据库创建嵌套数组
我有一个包含一些表和列的数据库。 我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不需要 array(array()) 来设置多维数组。只需将其他数组作为值推送即可。
试试这个:
you don't need to array(array()) to set up a multidimensional array. just push other array as a value.
try this:
这只是意味着您最终会得到一个如下所示的数组:
您不能像那样“设置多维数组”。它应该看起来更像这样:
有了这个,你最终会得到一个像这样的数组:
我猜你想要更多类似的东西:
所以你需要对列做这样的事情:
祝你实验更多好运有了这个...
var_dump
是你的朋友。This just means you end up with an array that looks like this:
You can't "set up a multi-dimensional array" like that. It should look more like this:
With this, you'd end up with an array like:
I guess you want more something along the lines of this though:
So you need to do something like this for the columns:
Good luck in experimenting more with this...
var_dump
is your friend.应该可以解决问题。
should do the trick.
您对列名称的查询不正确。您应该执行类似
SHOW COLUMNS FROM mytable FROM mydb;
或SHOW COLUMNS FROM mydb.mytable;
来源:http://dev.mysql.com/doc/refman/5.0/en/show-columns.html
你可能想做类似的事情:
虽然我没有机会运行此代码以查找错误。
Your query for column names isn't correct. You should do something like
SHOW COLUMNS FROM mytable FROM mydb;
orSHOW COLUMNS FROM mydb.mytable;
SOURCE: http://dev.mysql.com/doc/refman/5.0/en/show-columns.html
You probably want to do something like:
Although I haven't had a chance to run this code for errors.
您最好直接处理
INFORMATION_SCHEMA
并基于此创建数组:You're better off addressing the
INFORMATION_SCHEMA
directly and creating arrays based on that: