简体   繁体   English

接受用户的正则表达式并显示PHP数据库中的条目

[英]accept regular expression from user and display entries from database in PHP

This is in PHP. 这是在PHP中。 I am accepting a string in which user enters regular expression and I need to pass this in Select query to extract and display file names which matches the expression. 我接受用户在其中输入正则表达式的字符串,并且需要在Select查询中传递该字符串以提取和显示与表达式匹配的文件名。

For eg.if user enters *.pdf , it should return all pdf files; 例如,如果用户输入*.pdf ,它将返回所有pdf文件; if user enters ^temp, then all files starting with temp should be displayed and so on. 如果用户输入^ temp,则应显示所有以temp开头的文件,依此类推。

My string is reaching out properly, but i am not able to form the query. 我的字符串正确伸出,但是我无法构成查询。 I am storing a string in $parameter and the query is like this: 我在$ parameter中存储一个字符串,查询是这样的:

$result=mysql_query("select filename from fileinfo where filetype REGEXP '$parameter'");

Please help me out. 请帮帮我。

There are 2 problems I see 我看到两个问题

  1. You're using glob syntax (eg *.pdf) instead of an correct regexp syntax. 您使用的是glob语法(例如* .pdf),而不是正确的regexp语法。 '.*\\\\.pdf' would be a valid regexp equivalent of '*.pdf' (which should always fail, since it's invalid) '.*\\\\.pdf'将是与'*.pdf'等效的有效正则表达式(由于它无效,因此应始终失败)

  2. It looks like you are incorrectly testing FILETYPE instead of FILENAME? 看来您是错误地测试FILETYPE而不是FILENAME? Try this instead: 尝试以下方法:

    $result=mysql_query("select filename from fileinfo where filename REGEXP '{$parameter}'");

Finally, try echoing your SQL to ensure you're running the query you think you are. 最后,尝试回显您的SQL,以确保您正在运行自己认为的查询。 You may have some escaping issues. 您可能会遇到一些转义问题。

尝试这个

$result=mysql_query("select filename from fileinfo where filetype REGEXP '".$parameter."'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM