【问题标题】:Filling an HTML Table with Data from MySQL using PHP使用 PHP 用 MySQL 中的数据填充 HTML 表
【发布时间】:2014-11-24 03:28:20
【问题描述】:

我整天都在尝试让这个愚蠢的页面正常工作,但我无法弄清楚。我正在尝试从表中获取信息以显示在 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 机器编号问题描述 "; while($row = mysql_fetch_row($result)) { echo "" . $row['problem_id']. "" . $row['machine_id']. "" . $row[ '描述']. ""; } echo "[已解决?按钮在这里正确显示]"; /else { echo "没有问题! :)"; }/ ?>

【问题讨论】:

    标签: php html mysql


    【解决方案1】:

    您需要使用mysql_fetch_assoc() 而不是mysql_fetch_row() 才能使此代码正常工作。

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

    mysql_fetch_assoc() 将行作为数组获取,其元素具有关联键,例如 'abc'、'xyz'、...

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

    因此,第二个选项对你有用。

    【讨论】:

    • 我按照你说的做了,目前唯一的区别是输出的文本现在改为 mysql_fetch_assoc(。还有其他想法吗?
    【解决方案2】:

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

    【讨论】:

    • 文件名是.php,是一个大学项目,教授告诉我们用PHP,所以我假设它是为它设置的。
    • @JaysenStoudt 创建一个空的.php 脚本,其中只包含&lt;?php echo phpinfo(); ?&gt;,看看它是否执行并报告有关PHP 的信息。
    • 我在代码中的大约 3 个位置添加了“”,但在任何地方都看不到任何内容。你是这个意思吗?
    • @JaysenStoudt 如果您得到一个空白页面,则表示您的网络服务器上没有配置 PHP,请与您的教授/系统管理员联系。
    • 其实我发现了问题所在。而且我很笨。我试图通过简单地右键单击 PHP 文件并打开它来访问它。而不是通过网站访问它...谢谢您的帮助!
    【解决方案3】:

    详细说明编程学生所说的:使用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 函数的示例,请参阅here

    【讨论】:

      【解决方案4】:
      <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>
      

      【讨论】:

        【解决方案5】:

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

        <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();
            }
        }
        

        希望对你有帮助

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-11-06
          • 1970-01-01
          • 1970-01-01
          • 2018-04-13
          • 2016-07-03
          • 2018-03-12
          • 2012-05-18
          • 1970-01-01
          相关资源
          最近更新 更多