【问题标题】:PHP to Display MySQL results in HTML tablePHP 在 HTML 表中显示 MySQL 结果
【发布时间】:2014-12-04 05:56:07
【问题描述】:

所以我有一组有效的 PHP 代码,并试图在我的网站上显示它。 我只需要它显示为一个两列表,第一列是机器,第二列是状态。我不介意仅使用静态 html 或从数据库中提取此信息来显示机器列。感谢您的帮助。

<?php
    $host = "localhost"; 
    $user = "root"; 
    $pass = "hello"; 
    $db = "GYM"; 
    $r = mysql_connect($host, $user, $pass);

    if (!$r) {
        echo "Could not connect to server\n";
        trigger_error(mysql_error(), E_USER_ERROR);
        } else {
            echo "Connection established.\n";
        }

        $r2 = mysql_select_db($db);
        if (!$r2) {
            echo "Cannot select database\n";
            trigger_error(mysql_error(), E_USER_ERROR);
            } else {
                echo "Database selected.\n";
            }

            $query = "SELECT status FROM use_instance ORDER BY instance_id DESC LIMIT 1 ";

            $rs = mysql_query($query);

        if (!$rs) {
            echo "Could not execute query: $query";
            trigger_error(mysql_error(), E_USER_ERROR);
            } 

            while ($row = mysql_fetch_assoc($rs)) {
                echo $row['status'] ;
            }
        mysql_close();
        ?>

【问题讨论】:

  • mysql 已弃用。请改用mysqliPDO

标签: php html mysql


【解决方案1】:
<?php
$host = "localhost"; 
$user = "root"; 
$pass = "hello"; 
$db = "GYM"; 
$r = mysql_connect($host, $user, $pass);

if (!$r) {
    echo "Could not connect to server\n";
    trigger_error(mysql_error(), E_USER_ERROR);
    } else {
            echo "Connection established.\n";
    }

    $r2 = mysql_select_db($db);
    if (!$r2) {
            echo "Cannot select database\n";
            trigger_error(mysql_error(), E_USER_ERROR);
            } else {
                    echo "Database selected.\n";
            }

$query = "SELECT machine, status FROM use_instance ORDER BY instance_id DESC LIMIT 1 ";

            $rs = mysql_query($query);

    if (!$rs) {
            echo "Could not execute query: $query";
            trigger_error(mysql_error(), E_USER_ERROR);
            } 

            ?>
            <table>
                <tr>
                    <th>Machine</th>
                    <th>Status</th>
                </tr>

            <?php

            while ($row = mysql_fetch_assoc($rs)) {
                 ?>
                 <tr>
                    <td><?php echo $row['machine'];?></td>
                    <td><?php echo $row['status'];?></td>
                 </tr>
                 <?php
            }
    mysql_close();
    ?>
    </table>

【讨论】:

    【解决方案2】:

    您可以在 php 代码中编写 HTML 标签 只需在 echo 中编写 HTML 标签即可。

    例如

    echo"<table border = "1">";
    while ($row = mysql_fetch_assoc($rs)) 
        {
         echo " <tr><td>".$row['status']."</td></tr>";
         } 
       echo"</table >"; 
    

    【讨论】:

      【解决方案3】:

      首先你应该使用mysqli 扩展或PDO 而不是mysql,因为mysql 已被弃用。

      这段代码应该可以工作

      <?php
          $host = "localhost"; 
          $user = "root"; 
          $pass = "hello"; 
          $db = "GYM"; 
          $r = mysql_connect($host, $user, $pass);
      
          if (!$r) {
              echo "Could not connect to server\n";
              trigger_error(mysql_error(), E_USER_ERROR);
              } else {
                  echo "Connection established.\n";
              }
      
              $r2 = mysql_select_db($db);
              if (!$r2) {
                  echo "Cannot select database\n";
                  trigger_error(mysql_error(), E_USER_ERROR);
                  } else {
                      echo "Database selected.\n";
                  }
                  // add the machine name as a column to your query
                  $query = "SELECT machine, status FROM use_instance ORDER BY instance_id DESC LIMIT 1 ";
      
                  $rs = mysql_query($query);
      
              if (!$rs) {
                  echo "Could not execute query: $query";
                  trigger_error(mysql_error(), E_USER_ERROR);
                  } 
                 //start the table tag
                 echo '<table>';
      
                  while ($row = mysql_fetch_assoc($rs)) {
                      //display each row in the table
                      echo '<tr><td>'.$row['machine'].'</td><td>'.$row['status'].'</td></tr>' ;
                  }
                 //close the table tag
                 echo '</table>';
              mysql_close();
              ?>
      

      【讨论】:

        【解决方案4】:

        你也可以使用 mysqli_fetch_objects http://www.w3schools.com/php/func_mysqli_fetch_object.asp

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-02-22
          • 1970-01-01
          • 2023-04-10
          • 2011-09-28
          • 2012-12-07
          • 2017-03-14
          • 1970-01-01
          相关资源
          最近更新 更多