4

下面的代码可以很好地打印数据库表中的一条记录,但我真正想做的是以类似于我的代码的格式打印 mysql 表中的所有记录。

IE:字段名称作为 html 表中每一列的标题以及标题下方的条目。希望这对某人有意义;)

        $raw = mysql_query("SELECT * FROM tbl_gas_meters");
        $allresults = mysql_fetch_array($raw);
        $field = mysql_query("SELECT * FROM tbl_gas_meters");
        $num_fields = mysql_num_fields($raw);   
        $num_rows = mysql_num_rows($raw);   
        $i = 1;

            print "<table border=1>\n";
            while ($i < $num_fields)
            {
                echo "<tr>";
                echo "<b><td>" . mysql_field_name($field, $i) . "</td></b>";
                //echo ": ";
                echo '<td><font color ="red">' . $allresults[$i] . '</font></td>';
                $i++;
                echo "</tr>";
                //echo "<br>";
            }
            print "</table>";
4

5 回答 5

5

作为一条附加信息,您可能应该使用 PDO。它具有更多的功能,有助于学习如何准备 SQL 语句。如果您编写更复杂的代码,它也会为您提供更好的服务。

http://www.php.net/manual/en/intro.pdo.php

此示例使用对象而不是数组。不一定重要,但它使用的字符较少,所以我喜欢它。当您深入研究对象时,差异确实会出现,但在此示例中没有。

//connection information
$user = "your_mysql_user";
$pass = "your_mysql_user_pass";
$dbh = new PDO('mysql:host=your_hostname;dbname=your_db;charset=UTF-8', $user, $pass);

//prepare statement to query table
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
//loop over all table rows and fetch them as an object
while($result = $sth->fetch(PDO::FETCH_OBJ))
{
//print out the fruits name in this case.
  print $result->name;
  print("\n");
  print $result->colour;
  print("\n");
}

您可能还想查看准备好的语句。这有助于防止注射。出于安全原因,注入是不好的。这是该页面。

http://www.php.net/manual/en/pdostatement.bindparam.php

您可能还应该考虑清理您的用户输入。只是提醒一下,与您当前的情况无关。

还要使用 PDO 获取所有字段名称,试试这个

$q = $dbh->prepare("DESCRIBE tablename");
$q->execute();
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);

一旦你拥有了所有的表格字段,它会很容易使用<div>,甚至可以使用<table>你喜欢的方式来排列它们<th>

快乐学习 PHP。很好玩。

于 2013-02-12T05:01:44.243 回答
2

谢谢各位,明白了

        $table = 'tbl_gas_meters';
        $result = MYSQL_QUERY("SELECT * FROM {$table}");

        $fields_num = MYSQL_NUM_FIELDS($result);

        ECHO "<h1>Table: {$table}</h1>";
        ECHO "<table border='1'><tr>";
        // printing table headers
        FOR($i=0; $i<$fields_num; $i++)
        {
            $field = MYSQL_FETCH_FIELD($result);
            ECHO "<td>{$field->name}</td>";
        }
        ECHO "</tr>\n";
        // printing table rows
        WHILE($row = MYSQL_FETCH_ROW($result))
        {
            ECHO "<tr>";

            // $row is array... foreach( .. ) puts every element
            // of $row to $cell variable
            FOREACH($row AS $cell)
            ECHO "<td>$cell</td>";

            ECHO "</tr>\n";
        }
于 2013-02-13T03:21:22.080 回答
0
while ( $row = mysql_fetch_array($field) ) {
    echo $row['fieldname'];
    //stuff
}
于 2013-02-12T04:35:55.310 回答
0

尝试这个 :

    $raw = mysql_query("SELECT * FROM tbl_gas_meters");
    $allresults = mysql_fetch_array($raw);
    $field = mysql_query("SELECT * FROM tbl_gas_meters");
    while($row  = mysql_fetch_assoc($field)){
         echo $row['your field name here'];
    }

请注意,新的 php 版本不推荐使用 mysql_* 函数,因此请改用 mysqli 或 PDO。

于 2013-02-12T05:05:34.763 回答
0

谢谢!我调整了其中一些答案,以从任何表的所有记录中绘制一个表,而无需指定字段名称。只需将其粘贴到 .php 文件中并更改连接信息:

<?php    

// Authentication detail for connection
$servername = "localhost";
$username =   "xxxxxxxxxx";
$password =   "xxxxxxxxxx";
$dbname =     "xxxxxxxxxx";
$tablename =  "xxxxxxxxxx";
$orderby = "1 DESC LIMIT 500";  // column # to sort & max # of records to display

// Create & check connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);  // quit
    }

// Run query & verify success
$sql = "SELECT * FROM {$tablename} ORDER BY {$orderby}";
if ($result = $conn->query($sql)) {
    $conn->close();  // Close table
    $fields_num = $result->field_count;
    $count_rows = $result->num_rows;

    if ($count_rows == 0) {
        die ("No data found in table: [" . $tablename . "]" );  //quit 
        } 
    } else {
    $conn->close();  // Close table
    die ("Error running SQL:<br>" . $sql );  //quit
}

// Start drawing table
echo "<!DOCTYPE html><html><head><title>{$tablename}</title>";
echo "<style> table, th, td { border: 1px solid black; border-collapse: collapse; }</style></head>";
echo "<body><span style='font-size:18px'>Table: <strong>{$tablename}</strong></span><br>";  
echo "<span style='font-size:10px'>({$count_rows} records, {$fields_num} fields)</span><br>";
echo "<br><span style='font-size:10px'><table><tr>";        

// Print table Field Names
while ($finfo = $result->fetch_field()) {
    echo "<td><center><strong>{$finfo->name}</strong></center></td>";
}
echo "</tr>"; // Finished Field Names

/* Loop through records in object array */
while ($row = $result->fetch_row()) {
    echo "<tr>";    // start data row
    for( $i = 0; $i<$fields_num; $i++ ) {
        echo "<td>{$row[$i]}</td>";
    }
    echo "</tr>";   // end data row
}

echo "</table>";  // End table
$result->close();  // Free result set
?>
于 2017-09-15T11:11:39.570 回答