使用PHP从MySQL填充HTML表数据

时间:2014-11-24 03:28:20

标签: php html mysql

我整天都试着让这个愚蠢的页面发挥作用,我无法弄明白。我试图从表中获取信息以显示在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>";

我已经使用了多个网站试图让它发挥作用,但似乎没有任何效果。如果语法确实不合适,那是因为我在看到类似于我尝试的所有内容之后对其进行建模。

它应该做什么,如果表&#34; tbl_problem&#34;有数据,在网站上显示一个表格,显示问题ID,机器ID和描述,以及右侧的按钮,允许您删除&#34; tbl_problem&#34; (按钮现在是一个占位符,我还没有代码让它实际删除,但如果有人想指出我正确的方向,那我就非常感激!) 。

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

  

问题IDMachine NumberProblem描述&#34 ;; while($ row = mysql_fetch_row($ result)){echo&#34;&#34; 。 $行[&#39; PROBLEM_ID&#39;。 &#34;&#34; 。 $行[&#39; machine_id&#39;。 &#34;&#34; 。 $行[&#39;描述&#39]。 &#34;&#34 ;;回声&#34; [已解决?按钮在这里显示正确]&#34 ;; / else {echo&#34;没有问题! :)&#34 ;; } /?&gt;

5 个答案:

答案 0 :(得分:0)

您需要使用mysql_fetch_assoc()代替mysql_fetch_row()才能使此代码正常运行。

由于mysql_fetch_row()使用数字键将行提取为数组,例如0,1,2,......

并且mysql_fetch_assoc()将行作为数组提取,其元素使用关联键,例如&#39; abc&#39;,&#39; xyz&#39;,...

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

详细说明编程学生所说的:使用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

答案 3 :(得分:0)

<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>

答案 4 :(得分:0)

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

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

希望这有帮助