dssu33392 2014-12-16 07:14
浏览 430
已采纳

SQL语法出错,请检查与MySQL服务器版本对应的手册,以便在第1行的“ORDER BY类别ASC”附近使用正确的语法

Site has been working fine for years and all of a sudden I am getting this error. Any help from the experts would be greatly appreciated.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY category ASC' at line 1

Here is the code in question:

// SQL injection attack prevention function
$unit_Recordset1 = "";
if (isset($_GET['unit'])) {
  $unit_Recordset1 = GetSQLValueString($_GET['unit'], "text");
}
$category_Recordset1 = "";
if (isset($_GET['category'])) {
  $category_Recordset1 = GetSQLValueString($_GET['category'], "text");
}
else $_GET['category'] = "";

// Query builder that create single or multiple AND query
$sql = "SELECT * FROM documents WHERE ";
if(!empty($unit_Recordset1)) {$sql .= " unit = $unit_Recordset1 AND ";}
if(!empty($category_Recordset1)) {$sql .= " category = $category_Recordset1 AND ";}
// Remove the last AND
$sql = substr($sql, 0, -4);
if(!empty($category_Recordset1)) $sql .= " ORDER BY title ASC";
else $sql .= " ORDER BY category, title ASC";


// Query for left nav DISTINCT category values
$sqlnav = "SELECT DISTINCT category FROM documents WHERE unit = $unit_Recordset1 ORDER BY category ASC";


mysql_select_db($database_local, $local);

$Recordset1 = mysql_query($sql, $local) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

$Recordset2 = mysql_query($sqlnav, $local) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);
  • 写回答

2条回答 默认 最新

  • dongyan7851 2014-12-16 07:19
    关注

    There are flows where $unit_Recordset1 may be empty. In which case, the following statement:

    $sqlnav = "SELECT DISTINCT category FROM documents WHERE unit = $unit_Recordset1 ORDER BY category ASC";
    

    Will evaluate to:

    SELECT DISTINCT category FROM documents WHERE unit = ORDER BY category ASC
    

    Which, of course, isn't valid SQL. You need to add a check against this case too, something down the lines of:

    $unitClause = "";
    if(!empty($unit_Recordset1) {
        $unitClause = "WHERE unit = $unit_Recordset1 ";
    }
    $sqlnav = "SELECT DISTINCT category FROM documents $unitClause ORDER BY category ASC";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog