显示mysql表中的所有记录

时间:2013-02-12 04:33:39

标签: php mysql html-table

以下代码适用于从数据库表中打印一条记录,但我真正希望能够以类似于我的代码的格式打印mysql表中的所有记录。

I.E。:字段名称作为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>";

5 个答案:

答案 0 :(得分: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。这很有趣。

答案 1 :(得分: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";
        }

答案 2 :(得分:0)

while ( $row = mysql_fetch_array($field) ) {
    echo $row['fieldname'];
    //stuff
}

答案 3 :(得分: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'];
    }

请注意,mysql_ *函数在新的php版本中已弃用,因此请改用mysqli或PDO。

答案 4 :(得分: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
?>