我正在尝试通过其标题标题对表格进行排序。但是我得到了无法解析的查询。这是我在 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>