PHP、Mysql、Bootstrap 表不工作

标签 php mysql

我在脚本前坐了几个小时,但我发现它无法正常工作。我想用 mysql 数据填充 Bootstrap 表。不幸的是,我总是得到其他信息或错误消息。 如果有人能看看脚本就太好了。

$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
$sql = "SELECT Osman, Tanja, Christiane, Marcella, Magarita, Nathalie, Kommentar, Gutschein, EC FROM Salon1_10_04_2016";
$results = $conn->query($sql);

 <table class="table table-hover">
  <thead>
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Email</th>
  </tr>
</thead>
<tbody>
       <?while($row = mysqli_fetch_assoc($result)) {
            echo"<tr class='table_row'>";
            echo"<td>" . $row['Osman'] . "</td>";
            echo"<td>" . $row['Tanja'] . "</td>";
            echo"<td>" . $row['Christiane'] . "</td>";
            echo"<td>" . $row['Marcella'] . "</td>";
            echo"<td>" . $row['Magarita'] . "</td>";
            echo"<td>" . $row['Nathalie'] . "</td>";
            echo"<td>" . $row['Kommentar'] . "</td>";
            echo"<td>" . $row['Gutschein'] . "</td>";
            echo"<td>" . $row['EC'] . "</td>";
            echo"</tr>";
             } echo"</table>";}?>

最佳答案

你正在混合面向对象的风格和过程,试试这个:

<?php
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
$sql = "SELECT Osman, Tanja, Christiane, Marcella, Magarita, Nathalie, Kommentar, Gutschein, EC FROM Salon1_10_04_2016";
$results = $conn->query($sql);
?>
<table class = "table table-hover">
    <thead>
        <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>Email</th>
        </tr>
    </thead>
    <tbody>
        <?php
        while ($row = $results->fetch_assoc()) {
            echo"<tr class='table_row'>";
            echo"<td>" . $row['Osman'] . "</td>";
            echo"<td>" . $row['Tanja'] . "</td>";
            echo"<td>" . $row['Christiane'] . "</td>";
            echo"<td>" . $row['Marcella'] . "</td>";
            echo"<td>" . $row['Magarita'] . "</td>";
            echo"<td>" . $row['Nathalie'] . "</td>";
            echo"<td>" . $row['Kommentar'] . "</td>";
            echo"<td>" . $row['Gutschein'] . "</td>";
            echo"<td>" . $row['EC'] . "</td>";
            echo"</tr>";
        }
        ?>
    </tbody>
</table>

无论如何,表体中的列数与 thead 中定义的列数不同,请确保您的 SQL 有效。

更新:如果你想检查字符串中是否有值,你可以定义一个简单的函数,如果值为空则返回所需的字符:

<?php
function checkNull($str){
    if(!trim($str)) return '-';
    else return $str;
}

// how to use it:
echo"<td>" . checkNull($row['Osman']) . "</td>";
?>

关于PHP、Mysql、Bootstrap 表不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36650867/

相关文章:

php - WordPress:ACF 将行添加到转发器字段

php - 如何检查Mysql时间字段是否为NULL

mysql - MySQL 语句中的多个 WHERE 给我带来了麻烦

mysql - 在sql中停止或验证

python - 递归搜索和MySql比较

php - if isset SESSION 显示 div

php - 使用用户编辑帖子更新数据库

php - 没有任何内容会插入到我的数据库中

mysql - 如何在MySQL中进行多连接查询?

php - 在 MySQL 语句中返回\'