使用php从MYSQL数据库获取数据到html tabel

时间:2016-06-19 19:24:57

标签: php html mysql twitter-bootstrap

我设计了一个带有html(bootstrap样式)的表,现在我想从我的数据库表中获取数据并使用php在表中获取它,这是我的html代码: 谢谢你的帮助。 :)

            <div class="ibox-content">
            <table class="table table-striped table-bordered table-hover dataTables-example" >
            <thead>
            <tr>
                <th>Category</th>
                <th>Register Date</th>
                <th>Occurrence Date</th>
                <th>Province</th>
                <th>User</th>
            </tr>
            </thead>
            <tbody>
            <tr class="gradeX">
                <td>test</td>
                <td>test</td>
                <td>test</td>
                <td class="center">test</td>
                <td class="center">test</td>
            </tr>
            </tfoot>
            </table>
            </div>
        </div>

PHP代码我试过,但我没有得到任何结果它给我错误:

<?php
      $username = "root";
      $password = "=";
      $host = "localhost";

      $connector = mysql_connect($host,$username,$password)
          or die("Unable to connect");
        echo "Connections are made successfully::";
      $selected = mysql_select_db("user", $connector)
        or die("Unable to connect");

      //execute the SQL query and return records
      $result = mysql_query("SELECT * FROM table_one ");
      ?>
            <div class="ibox-content">
            <table class="table table-striped table-bordered table-hover dataTables-example" >
            <thead>
            <tr>
                <th>Category</th>
                <th>Register Date</th>
                <th>Occurrence Date</th>
            </tr>
            </thead>
            <tbody>
            <tr class="gradeX">
                <td>test</td>
                <td>test</td>
                <td>test</td>
                <td class="center">test</td>
                <td class="center">test</td>
            </tr>
            </tfoot>
            </table>
            </div>
        </div>
        <?php
          while( $row = mysql_fetch_assoc( $result ) ){
            echo
            "<tr>
          echo "<td>" . $row['Category'] . "</td>";
              <td>{$row\['regdate'\]}</td>
              <td>{$row\['occdate'\]}</td>
            </tr>\n";
          }
        ?>

1 个答案:

答案 0 :(得分:0)

由于mysql_*已弃用,我将使用mysqli_*回答此问题。

在循环读取数据之前,您已经关闭了示例中的表,因此我不确定 您希望这些行填充,但我猜了。

你尝试的另一个错误是你有回音“... echo”“;

<?php
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
        . mysqli_connect_error());
}

$query = 'SELECT * FROM table_one';
$data = mysqli_query($mysqli, $query);
?>

<div class="ibox-content">
    <table class="table table-striped table-bordered table-hover dataTables-example" >
        <thead>
            <tr>
                <th>Category</th>
                <th>Register Date</th>
                <th>Occurrence Date</th>
            </tr>
        </thead>
        <tbody>
<?php
while($row = mysqli_fetch_array($data)) 
{
    echo "  <tr>
                <td>" . $row['Category'] . "</td>
                <td>" . $row['regdate'] . "</td>
                <td>" . $row['occdate'] . "</td>
            </tr>";
}
?>
        </tbody>
        <tfoot>
        </tfoot>
    </table>
</div>

我在while循环中缩进了echo,以使其与其他html内联。