php 在for循环PHP中获取列名和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13583367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:43:18  来源:igfitidea点击:

Get column names and values in for loop PHP

phpmysql

提问by Tamara

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

}  

result: naam=Ketting

结果:naam=Ketting

回答by Mirko Akov

Actually really easy solution to your problem would be to use foreach loop, like so:

实际上,您的问题的真正简单解决方案是使用 foreach 循环,如下所示:

while($row = mysql_fetch_assoc($query))
{
    foreach($row as $key => $value)
    {
        echo "$key=$value";
    }
}

回答by Michael

mysql_fetch_assocreturns an associative array. You can use a foreachloop to retreive the key=>value of each item in the array.

mysql_fetch_assoc返回一个关联数组。您可以使用foreach循环来检索数组中每个项目的 key=>value。

$sql = "SELECT * FROM products WHERE modItemID= 14"; 
$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)){
    foreach($row as $column=>$value) {
      echo "$column = $value\n";
    }
    echo $fieldName."=".$row[$fieldName];   
  }

}  

回答by dipenparmar12

Just Put your Database name,username,password and table name.

只需输入您的数据库名称、用户名、密码和表名。

You get all data from your main database (with column name)

您从主数据库中获取所有数据(带有列名)

<?php 

function qry($q){

    global $qry;
    try {   
    $host = "?";
    $dbname = "?";
    $username = "?";
    $password = "?";
    $dbcon = new PDO("mysql:host=$host; 
    dbname=$dbname","$username","$password");
}
catch (Exception $e) {

    echo "ERROR ".$e->getMEssage();

}

    $qry = $dbcon->query($q);
    $qry->setFetchMode(PDO:: FETCH_OBJ);

    return $qry;

}


echo "<table>";

/*Get Colums Names in table row */
$columns = array();

$qry1= qry("SHOW COLUMNS FROM Your_table_name");

while (@$column = $qry1->fetch()->Field) {
    echo "<td>".$column."</td>";
    $columns[] = $column;

}

echo "<tr>";

/* Fetch all data into a html table *

$qry2 = qry("SELECT * FROM Your_table_name");

while ( $details = $qry2->fetch()) {

    echo "<tr>";
    foreach ($columns as $c_name) {
    echo "<td>".$details->$c_name."</td>";

}

}

echo "</table>";

?>