MYSQL重新获取错误

时间:2012-01-05 02:11:36

标签: mysql sql database forms syntax

我刚收到MYSQL的错误说“您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以便在第1行附近使用正确的语法”

在页面上提交表格后,我得到了那个消息。

有什么想法吗?

这是我使用的SQL代码

$sql = "SELECT * FROM member ".
                "WHERE termId='$term'  ".
                "AND year='$year' ".
                "AND familyId='$familyId' order by memberId";
        $rs =  mysql_query($sql) or die(mysql_error()); 

3 个答案:

答案 0 :(得分:0)

始终使用mysql_real_escape_string来转义字符串中的特殊字符。

$term=mysql_real_escape_string($term);

if(isset($year,$familyid,$term))
{
  $myear=intval($year);  
  $mfamilyid=intval($familyid);

  $sql = "SELECT * FROM member WHERE termId='$term' 
            AND year='$myear' 
            AND familyId='$mfamilyId' 
            order by memberId";

  $rs =  mysql_query($sql) or die(mysql_error()); 
 }

答案 1 :(得分:0)

您可能需要使用反引号替换查询中的单引号,如下所示。

$sql = "SELECT * FROM member ".
        "WHERE termId=`$term` ".
        "AND year=`$year` ".
        "AND familyId=`$familyId` order by memberId";
$rs =  mysql_query($sql) or die(mysql_error());

答案 2 :(得分:0)

你的最终SQL是

SELECT * FROM member_class WHERE termId='2' AND year='' AND familyId='' order by memberId

我怀疑至少是familyID,也许年份也是数字字段,所以这是语法错误。