繁体   English   中英

带有特定字段的Echo HTML表

[英]Echo html table with specific field

我是PHP的新手。 而且我似乎在这里找不到我的代码的问题。 我正在尝试从表中选择特定的行,并使用自己的标头在HTML表中回显它。 我一直在表消息中获取无可用数据。

<?php
       $db_host = 'localhost';
       $db_user = 'my user';
       $db_pwd = 'my pwd';

       $database = 'my db';
       $table = 'client';

    if (!mysql_connect($db_host, $db_user, $db_pwd))
                    die("Can't connect to database");

                if (!mysql_select_db($database))
                    die("Can't select database");

                // sending query
                $result = mysql_query("SELECT 'id', 'datecreated', FROM {$table}");
                if (!$result) {
                    die("Query to show fields from table failed");
                }

                $fields_num = mysql_num_fields($result);

                echo "<table class='table table-bordered table-striped mb-none' id='datatable-tabletools' data-swf-path='assets/vendor/jquery-datatables/extras/TableTools/swf/copy_csv_xls_pdf.swf' >";
                // printing table headers
                echo "<thead>";
                echo "<th>Client ID</th>";
                echo "<th>Sign Up Date</th>";
                echo "</thead>";
                // printing table rows
                if ($results->num_rows > 0) {
                while ($row = $results->fetch_assoc()){

                }
                    echo "<tbody>";
                    echo "<tr>";


                    echo "<td>".$row["id"]."</td>";
                    echo "<td>".$row["datecreated"]."</td>";



                    echo "</tr>";
                    echo "</tbody>";
                }
                mysql_free_result($result);
                ?>

将while循环周围的代码顺序更改为:

if ($fields_num > 0) {
    echo "<tbody>";
    while ($row = mysql_fetch_assoc($result)) {

        echo "<tr>
            <td>".$row["id"]."</td>
            <td>".$row["datecreated"]."</td>
            </tr>";

    }
    echo "</tbody>";
}

目前,您实际上并没有对要检索的数据做任何事情。

和仅供参考-mysql已过时,您应该使用mysqli

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM