1

多くの異なるデータベーステーブルのすべてのフィールドをHTMLで出力するPHP関数を作成しようとしています。テーブルは行も列も異なるので、メソッドを考え出そうとしています。

ここの例のように-PHPを使用して列のすべての値を取得するにはどうすればよいですか?

ただし、データベースの各行にある列の数やその名前がわからない場合は、"SELECT * FROM $mytable"各データベースの列と行が異なる場合にクエリからデータを出力するにはどうすればよいですか?

4

5 に答える 5

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のバージョンによって異なります)。代わりに、 PDOまたはmysqliを使用する必要があります

于 2012-09-13T19:13:36.797 に答える
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);
于 2012-09-13T19:45:11.597 に答える
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);
    }
}
于 2013-09-06T06:51:51.130 に答える
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>';
}
于 2014-02-01T15:44:46.680 に答える
-1

SQLコマンド"SHOW TABLES"を使用して、データベース内のすべてのテーブルのリストを取得できます。

次に、結果をループして、各テーブルのフィールドを照会できます。

于 2012-09-13T19:12:51.607 に答える