使用 PHP 用来自 MySQL 的数据填充 HTML 表


Filling an HTML Table with Data from MySQL using PHP

我一整天都在努力让这个愚蠢的页面工作,但我无法弄清楚。我正在尝试从表中获取信息以显示在 php 文件的表中。

if (!mysql_connect($servername, $username, $password))
  die("Connection failed: ");
if (!mysql_select_db($dbname))
  die("Can't select database");
$result = mysql_query("SELECT problem_id, machine_id, description FROM tbl_problem");
if (!$result)
{
  die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($result);
echo "<table class='table table-hover'><tr><th>Problem ID</th><th>Machine Number</th><th>Problem Description</th><th>     </th></tr>";
while($row = mysql_fetch_row($result))
{
  echo "<tr><td>" . $row['problem_id']. "</td><td>" . $row['machine_id']. "</td><td>" . $row['description']. "</td><td><button type='button' class='btn btn-danger'>Resolved?</button></td></tr>";
}
echo "</table>";

我已经使用过几个网站来尝试让它工作,但似乎没有任何效果。如果语法真的不正常,那是因为我是在我所看到的与我试图做的事情类似的一切之后对其进行建模的。

它应该做的是,如果表"tbl_problem"中有数据,则在网站上显示一个表,显示问题 ID、机器 ID 和描述,以及允许您从"tbl_problem"中删除行的右侧的按钮(该按钮现在作为占位符存在, 我还没有实际删除它的代码,但如果有人想指出我正确的方向,我会非常有义务!

当我运行它时,我没有得到任何表,而是看到以下内容:

问题 ID 编号问题描述 ";而($row = mysql_fetch_row($result)) { echo " .$row['problem_id']。'"'" .$row['machine_id']。'"'" .$row["描述"]。'"'"; }回声"[解决?按钮在此处正确显示]";/else { echo "There are no problem! :)"; }/?>

你需要使用 mysql_fetch_assoc() 而不是 mysql_fetch_row() 来让这段代码工作。

As mysql_fetch_row() 将行作为带有数字键的数组获取,例如 0,1、2,....

mysql_fetch_assoc()将行作为数组及其元素与关联键一起获取,例如"abc"、"xyz",...

在您的代码中,您期望数组及其元素具有关联键。

因此,第二种选择将为您工作。

如果您在

浏览器窗口中看到PHP源代码输出,则意味着您的文档未作为PHP脚本执行,因此PHP未安装或配置错误(您是否使用.php作为文件扩展名,并且您的Web服务器是否配置为使用PHP来处理这些文件?

详细阐述一下编程学生所说的话:使用mysqli_fetch_assoc()是一种常见的做法。但是,mysql_fetch_assoc()在较新版本的 PHP 中已弃用。

while($row = mysqli_fetch_assoc($result))
{
  echo "<tr><td>".$row['problem_id']."</td><td>".$row['machine_id']."</td></tr>";
}

有关mysqli函数的示例,请参阅此处。

<table class="table table-bordered table-condensed" style="width:500px" >
    <thead style="background-color:#09C;color:#FFF">
    <tr>
        <th> Name </th>
        <th> Age </th>
    </tr>
    </thead>
    <tbody>
    <?php
        $activities = mysqli_query($con,"select * from user where roleid = 1 order by active desc");
        while($activity = mysqli_fetch_array($activities))
        {
    ?>

        <tr>
            <td> <a href="<?php echo $editlink ?>" style="text-decoration:none"> <?php echo $activity['name'] ?> </a> </td>
            <td> <?php echo $activity['age'] ?> </td>
        </tr>
    <?php
        }
    ?>
    </tbody>
</table>

在您的主页中尝试添加此代码

<table class="table table-condensed table-bordered">
                    <tr>
                        <th>
                            Name of Column 1
                        </th>
                        <th>
                            Name of Column 2
                        </th>
                        <th>
                            Name of Column 3
                        </th>           
                    </tr>
                        <?php
                            Name::listName();
                            //$sql
                        ?>

在你的类中(假设你有一个)添加这个

<?php
class Name{
  public $yourcol1;
  public $yourcol2;
  public $yourcol3;
 public function __construct(){
    $this->yourcol1=''; //assumed all are strings
    $this->yourcol2='';
    $this->yourcol3='';
 }
public static function listName(){
    $qry="select * from tbl_yourtablename;";
    $cResult=mysql_query($qry);
    if($cResult){
        while($cRows=mysql_fetch_array($cResult)){
            echo '<tr><td>'.$cRows['yourcol1'].'</td>';
            echo '<td>'.$cRows['yourcol2'].'</td>';
            echo '<td>'.$cRows['uourcol3'].'</td></tr>';
        }
    }else{
        ShowDbError();
    }
}

希望这有帮助