1

我正在尝试通过其标题标题对表格进行排序。但是我得到了无法解析的查询。这是我在 MySQL 中做错了什么吗?

我必须订购一张包含数千条记录的行话表。所以我只是在家里练习它以使其正确。然而,目前我似乎被困在那个 MySQL 位上。我遵循了另一个网站上的提示,该网站有此代码,但仍然卡住解释为什么这不起作用会有所帮助。

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="../demo.css" rel="stylesheet" type="text/css" />
</head>
<body>

    <?php
    function makeHeaderLink($value, $key, $col, $dir) {
        $out = "<a href=\"" . $_SERVER['SCRIPT_NAME'] . "?c=";
        //set column query string value
        switch($key) {
            case "Acronym":
                $out .= "1";
                break;
            case "Full Name":
                $out .= "2";
                break;
            case "What":
                $out .= "3";
                break;
            default:
                $out .= "0";
        }

        $out .= "&d=";

        //reverse sort if the current column is clicked
        if($key == $col) {
            switch($dir) {
                case "ASC":
                    $out .= "1";
                    break;
                default:
                    $out .= "0";
            }
        }
        else {
            //pass on current sort direction
            switch($dir) {
                case "ASC":
                    $out .= "0";
                    break;
                default:
                    $out .= "1";
            }
        }

        //complete link
        $out .= "\">$value</a>";

        return $out;
    }

    switch($_GET['c']) {
        case "1":
            $col = "Acronym";
            break;
        case "2":
            $col = "Full_Name";
            break;
        case "3":
            $col = "What";
            break;
                }

    if($_GET['d'] == "1") {
        $dir = "DESC";
    }
    else {
        $dir = "ASC";
    }

    if(!$link = mysql_connect("localhost", "username", "password")) {
        echo "Cannot connect to db server";
    }
    elseif(!mysql_select_db("nazir_jargon")) {
        echo "Cannot select database";
    }
    else {

        if(!$rs = mysql_query("SELECT 'Acronym', 'Full Name', 'What' * FROM jargon1 ORDER BY $col $dir")) {
            echo "Cannot parse query";
        }
        elseif(mysql_num_rows($rs) == 0) {
            echo "No records found";
        }
        else {
            echo "<table class=\"bordered\" cellspacing=\"0\">\n";
            echo "<tr>";
            echo "<th>" . makeHeaderLink("Acronym", "Acronym", $col, $dir) . "</th>";
            echo "<th>" . makeHeaderLink("Full Name", "Full_Name", $col, $dir) . "</th>";
            echo "<th>" . makeHeaderLink("What", "What", $col, $dir) . "</th>";
            echo "</tr>\n";
            while($row = mysql_fetch_array($rs)) {
                echo "<tr><td>$row[person_id]</td><td>$row[Acronym]</td><td>$row[Full_Name]</td><td>$row[What]</td></tr>\n";
            }
            echo "</table><br />\n";
        }
    }
    ?>
</body>

4

2 回答 2

1

从查询中删除单引号 & *,以便查询应如下所示。我希望它会奏效。

SELECT Acronym, Full Name, What FROM jargon1 ORDER BY $col $dir

如果它不起作用,

可以打印吗

echo "SELECT Acronym, Full Name, What FROM jargon1 ORDER BY $col $dir";

让我知道你得到了什么?

注意:我认为您想使用反引号(`),但是错误地使用了单引号。


编辑 1

如你所见

echo "SELECT Acronym, Full Name, What FROM jargon1 ORDER BY $col $dir";

印刷

SELECT Acronym, Full Name, What FROM jargon1 ORDER BY ASC`

这意味着$col没有打印..请检查出了什么问题$col

SELECT Acronym, Full Name, What FROM jargon1 ORDER BY columnNameMissing ASC`
                                                      ^^^^^^^^^^^^^^^^^
于 2012-12-08T12:57:44.533 回答
-1

如果您指定返回的列,请从您的选择查询中删除 *。否则用 *. 非此即彼。

于 2012-12-08T12:18:52.623 回答