MySql和PhP - 为db列创建排名下拉列表

时间:2011-12-02 14:08:09

标签: php mysql

我正在尝试对moduleID进行列计数,以便我可以将下拉列表中的模块排序为1,2,3而不是moduleID。我想要计数,以便我不能排列比可用的模块更多的模块。这是我到目前为止所做的:但排名没有出现:

`<`!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Module Selector</title>
        <style>
            tr {background-color:lightblue;}
            td {text-align:center;}
    </style>
    </head>
    <body>

        <?php
            require_once "includes/connection.inc.php";
            $conn = dbConnect();
            //echo 'connected';
                $sql = "SELECT * FROM module";
                $nRows ="select moduleID, count(moduleID) from module"; 
                $stmt = $conn->prepare($sql);
            try {
                $stmt->execute();
                $results = $stmt->fetchAll();
                if (!$results){ // check we have some results
                    echo "No modules Available at this Time try again later <br />";
                }
                else{   //generate table of modules
                    print "<table>\n";
                    echo "<th>ModuleID</th><th>Name</th><th>Description</th><th>Lecturer</th><th>Ranking</th>";
                    foreach ($results as $row){
                        echo "<tr>";
                        echo "<td>".$row["moduleID"]."</td>";
                        echo "<td>".$row["ModuleName"]."</td>";
                        echo "<td>".$row["ModuleDesc"]."</td>";
                        echo "<td>".$row["LecturerID"]."</td>";
                        echo "<td>".$row[ $nRows]."</td>";
                    }
                    print "</table>\n";
                }
            } catch ( PDOException $e ) {
                echo "Query failed: " . $e->getMessage();
            }
            // close database connection
            dbClose($conn);
        ?>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

试试这个

$sql = "SELECT *, 
               (SELECT COUNT(moduleID) 
                FROM module 
                WHERE moduleID = 'MODULEID') as count 
       FROM module";
$get = mysql_query($sql);

现在您可以使用

获取计数
$get_row['count']