高级搜索mysql perl

t a*_*t a -3 mysql perl search-engine

我是新手.我已经搜索了Stackoverflow几周,试图找到高级搜索的代码.
终于创造了我自己的.它工作得很好.我在这里列出了其他可能想要使用它的人.
问题是:

  • 当没有选择item_category时我默认 * 但它不起作用.item_category是搜索所需的唯一变量?

  • 我是否需要关注消毒或黑客注射?

  • 完全匹配似乎不起作用.它可能正在搜索100个单词"FREE"并且找不到匹配项.有任何想法吗?

  • 有关可靠性和速度问题的建议吗?

    $ ANDOR = param('andor');#创建AND OR搜索if($ ANDOR eq""){$ ANDOR ="AND";}

    if($ item_category){$ item_category ="$ item_category"; } else {$ item_category ="*"; } $ statement.="item_category LIKE'$ item_category'";

    if($ item_state){$ statement.="$ ANDOR item_state LIKE'$ item_state'"; } if($ item_city){$ statement.="$ ANDOR item_city LIKE'$ item_city'"; } if($ db_id){$ statement.="$ ANDOR db_id ='$ db_id'"; }

    $关键字=参数( '关键字');

    if($ keywords){if(param('searchmatch')eq"exact"){$ statement.="$ ANDOR item_name = \"$ keywords \"OR item_desc = \"$ keywords \"OR item_desc2 = \"$关键字\"";#

    } else {$ statement.="$ ANDOR"; 我的@keywords = split(/ /,$ keywords);

    foreach my $keyword(@keywords)
    {
    $statement .= " item_name LIKE '%$keyword%' 
    OR item_desc LIKE '%$keyword%' 
    OR item_desc2 LIKE '%$keyword%' ";
    }
    
    Run Code Online (Sandbox Code Playgroud)

    }}

    $ date_begin =参数( 'date_begin'); if($ date_begin){$ statement.="$ ANDOR modification_time>'$ date_begin'"; }

    if($ user){$ statement.="$ ANDOR user LIKE'$ user'"; }

    $ price_low的=参数( 'price_low的'); $ price_high =参数( 'price_high'); 如果(($ price_low的)&&(price_high $)){$声明= "$ ANDOR ITEM_PRICE BETWEEN '$ price_low的' 和 '$ price_high'".} elsif(($ price_low)&&($ price_high eq"")){$ statement.="$ ANDOR item_price BETWEEN'$ price_low'AND*"; } elsif(($ price_high)&&($ price_low eq"")){$ statement.="$ ANDOR item_price BETWEEN'0'AND'$ price_high'"; } else {$ statement.="";

    }

    我的$ sth = $ dbh-> prepare(qq(SELECT*FROM table WHERE $ statement))或die $ DBI :: errstr; $ sth->执行(); while((my(@rows))= $ sth-> fetchrow_array){$ total_row_count = $ sth-> rows; $ database_rows = join("\ |",@rows); push(@ database_rows,$ database_rows); }

kbe*_*son 5

  1. ' '不是where子句的有效条件.你可以把"1"代替" ",但更好的是只留下where子句的那一部分.

  2. 是; 使用参数绑定.例如

    if ($item_state) {
        $statement .= " $ANDOR item_state LIKE ? ";
        push(@binds, $item_state);
    }
    
    Run Code Online (Sandbox Code Playgroud)

    然后让DBI正确清理并包含你的参数:

    $sth->execute(@binds);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 检查生成的完整查询.我怀疑如果你看看AND/OR优先规则它没有按照你的想法行事,你可以通过明智地使用括号来逃避.

  4. 不要这样做.在CPAN上查找为您做后端工作的事情,并使用适当的参数调用它.在您使用其他人为此编写的工具之后,您将对所有未处理的案例有一个很好的了解,然后可以考虑如果您决定重新访问时如何添加您认为缺少的内容再次进行搜索的想法.

在这一点上,我写了一个简单的数据库查询引擎/微小的ORM大约4次,其中3个在perl中.每当我学到新的东西,但更重要的是每当我记得为什么我应该使用其他已经找到所有边缘情况的人的解决方案.