簡體   English   中英

選擇並顯示MySQL表中的所有字段以獲取無限量的PHP列

[英]Select and Display All Fields from MySQL Table for indefinite amount of columns with PHP

我正在嘗試編寫一個PHP函數,該函數將為許多不同的數據庫表輸出HTML中的所有字段。 這些表在行和列中都不同,所以我試圖提出一種方法。

就像這里的示例一樣- 如何使用PHP獲取列中的所有值?

但是,如果我不知道數據庫中每行中有多少列,或者它們的名稱,當每個數據庫的行和行不同時,如何從"SELECT * FROM $mytable"查詢中輸出數據?

也許是這樣的嗎?

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

這將建立一個表,表頭行包含列名。 在某些地方可能更雄辯,但如果我理解您的要求,則可以采用骯臟而骯臟的解決方案。

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

我是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>';
}

您可以使用SQL命令"SHOW TABLES"來獲取數據庫中所有表的列表。

然后,您可以遍歷結果並查詢每個表的字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM