PHP在HTML表格中显示MySQL结果

时间:2014-12-04 05:56:07

标签: php html mysql

所以我有一组有效的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();
        ?>

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

首先你应该使用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();
        ?>

答案 2 :(得分:0)

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

例如

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

答案 3 :(得分:0)

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