在for循环PHP中获取列名和值 [英] Get column names and values in for loop PHP
本文介绍了在for循环PHP中获取列名和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从特定表中获取所有列名和值.此表中的字段不是固定的,因此有时用户会添加一个字段,也可以删除它们.我想要做的是检索所有列名并获取它的值.就我现在而言,正在检索列名并从中获取一个值.我知道为什么我会得到这个结果,但我不知道如何解决它.
I would like to get all column names and values from a specific table. The fields in this table aren't fixed, so sometimes there will be added one field by the user and they also can be removed. What I want to do is retrieve all column names and getting the values of it. As far as I am now is retrieving the column names and getting one value out of it. I know why I get this result, but I don'tknow how to fix it.
//I edited this sql, normally the table value and id are variable
$sql = "SELECT * FROM ".$_POST['moduleName']." WHERE modItemID= ".$_POST['modItemID'];
$query = mysql_query($sql);
$columns = mysql_num_fields($query);
for($i = 1; $i < $columns; $i++) {
//read field name
$fieldName = mysql_field_name($query,$i);
while($row = mysql_fetch_assoc($query,$i)){
echo $fieldName."=".$row[$fieldName];
}
}
结果:naam=Ketting
result: naam=Ketting
推荐答案
实际上很容易解决你的问题是使用 foreach 循环,像这样:
Actually really easy solution to your problem would be to use foreach loop, like so:
while($row = mysql_fetch_assoc($query))
{
foreach($row as $key => $value)
{
echo "$key=$value";
}
}
这篇关于在for循环PHP中获取列名和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文