我正在尝试通过其标题标题对表进行排序。但是我得到了无法解析的查询。这是我在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>
答案 0 :(得分:1)
删除单引号&amp;来自查询的 * ,以便查询应如下所示。我希望它能奏效。
SELECT Acronym, Full Name, What FROM jargon1 ORDER BY $col $dir
如果不起作用,
可以打印
echo "SELECT Acronym, Full Name, What FROM jargon1 ORDER BY $col $dir";
让我知道你得到了什么?
注意:我认为你想使用反引号(`),但错误地使用单引号。
如你所见
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`
^^^^^^^^^^^^^^^^^
答案 1 :(得分:-1)
如果要指定返回的列,请从选择查询中删除*。否则用*来取回一切。一个或另一个。