使用PHP选择并显示MySQL表中的所有字段,以获得无限量的列

时间:2012-09-13 19:03:44

标签: php mysql

我正在尝试编写一个PHP函数,它将输出HTML中所有不同数据库表的字段。这些表的行数和列数各不相同,所以我想尝试一种方法。

就像这里的例子一样 - How get all values in a column using PHP?

如果我不知道数据库中每行中有多少列或它们的名称,当每个数据库在列和行中变化时,如何从"SELECT * FROM $mytable"查询输出数据?< / p>

5 个答案:

答案 0 :(得分:5)

也许是这样的?

echo "<table>";

while ($row = mysql_fetch_array($query))
{
    echo "<tr>";

    foreach($row as $value)
    {
        echo "<td>".$value."</td>";
    }

    echo "</tr>";

}

echo "</table>";

<强>更新

由于这个答案似乎仍然得到了一些关注,我觉得它值得注意的是,因为它是基于一个现在过时的例子(在原始问题中链接),这个答案本身也已经过时了。这是因为使用了mysql扩展名,现在已弃用/删除(取决于您的PHP版本)。相反,您应该使用PDOmysqli

答案 1 :(得分:3)

这将构建一个包含列名称的标题行的表。在某些地方可能会更有说服力,但如果我理解你的要求,那就是肮脏的解决方案。

function tableme($result){
    $header='';
    $rows='';
    while ($row = mysql_fetch_array($result)) { 
        if($header==''){
            $header.='<tr>'; 
            $rows.='<tr>'; 
            foreach($row as $key => $value){ 
                $header.='<th>'.$key.'</th>'; 
                $rows.='<td>'.$value.'</td>'; 
            } 
            $header.='</tr>'; 
            $rows.='</tr>'; 
        }else{
            $rows.='<tr>'; 
            foreach($row as $value){ 
                $rows .= "<td>".$value."</td>"; 
            } 
            $rows.='</tr>'; 
        }
    } 
    return '<table>'.$header.$rows.'</table>';
}

只需使用您的$result来查询;

 echo tableme($result);

答案 2 :(得分:0)

$query = "SELECT * FROM `ios7_google`";

$result = mysql_query("SHOW COLUMNS FROM `ios7_google`.`graph_geo_table`");
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
if (mysql_num_rows($result) > 0) {
    while ($row = mysql_fetch_assoc($result)) {
    mssql_field_name($result,0);
    //    print_r($row);
    }
}

答案 3 :(得分:0)

我是PHP的新手,并赞赏Dave(由Patrick Q编辑)之前的帖子。我已经在下面的mysqli版本中构建了他们的示例。提供大量评论以帮助其他人了解正在发生的事情......作为一项功能,我现在已将其包含在每个具有表格输出的页面中。

/*
Function: tabular_output (mysqli_query) as string
Utilization: Given the results of a successful mysqli_query, produce a dynamic table based on the columns identified.
<th> contents are based of the fields of the array.
<td> contents are based on contents associated to the relevant fields.
The table is not limited in size or length and table attributes can be controlled external to the function via CSS

Ref: http://www.php.net/manual/en/mysqli-result.fetch-fields.php
Ref: http://stackoverflow.com/questions/12413064/select-and-display-all-fields-from-mysql-table-for-indefinite-amount-of-columns
*/

function tabular_output($sql_result){
//Initiate header and rowdata for the table output.
$header='';
$rowdata='';

//Initiate header string.
$header='<tr>';
    //Assign the $sql_result field attributes to the $field array to output column headings.
    $field= mysqli_fetch_fields($sql_result);

    //Iterate through the field array to produce the name value in the header.
    foreach ($field as $val) {
        $header.= "<th>".$val->name."</th>";
    }

//Header string complete.
$header.='</tr>';

//Iterate through the data ($row) contained within the passed $sql_result.
while ($row = mysqli_fetch_array($sql_result))
{
    //Assign the $sql_result field attributes to the $field array to dynamically handle data contents.
    $field= mysqli_fetch_fields($sql_result);

    //Initiate the rowdata string.
    $rowdata.='<tr>';

    //Iterate through the field array to produce the associated $rowdata element.
    foreach ($field as $val) {
        $rowdata.= "<td>".$row[$val->name]."</td>";
    }
    $rowdata.='</tr>';

} //Rowdata string complete.
//Return the finished table string to the referring procedure.
return '<table>'.$header.$rowdata.'</table>';
}

答案 4 :(得分:-1)

您可以使用SQL命令"SHOW TABLES"获取数据库中所有表的列表。

然后,您可以遍历结果并查询每个表的字段。