PHP MySQL命令表代码错误“无法解析查询” [英] PHP MySQL order table code error "cannot parse query"

查看:154
本文介绍了PHP MySQL命令表代码错误“无法解析查询”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get a table to sort via its header title. But I get the cannot parse query. Is it something I'm doing wrong in the MySQL?

我必须订购一个包含数千个记录的工作表。所以我只是在家里练习它以使其正确。但是目前我似乎陷入了那个MySQL位。我按照这个代码的不同网站上的提示,但仍然被解释为什么这不起作用将有所帮助。

I have to order a table of Jargon for work which contains thousands of records. So I'm just practising it at home to get it right. However at the moment I seem to be getting stuck on that MySQL bit. I followed the tips on a different website which had this code but still getting stuck a explanation of why this isn't working will help.

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

推荐答案

删除单引号&来自查询的 * ,以便查询应如下所示。我希望它能奏效。

Remove single quote & * from the query so that query should look like below. I hope it will work.

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

如果不起作用,

可以打印

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

让我知道你得到了什么?

and let me know what you get?

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

Note: I think you wanted to use backticks(`), however mistakenly used single quote.

如您所见

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

打印

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

这意味着 $ col 没有打印..请检查 $ col

That means $col is not printing.. please check what is going wrong with $col

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

这篇关于PHP MySQL命令表代码错误“无法解析查询”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆