Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用REGEXP搜索三个字段并避免空子字符串错误_Php_Mysql_Regex - Fatal编程技术网

Php 使用REGEXP搜索三个字段并避免空子字符串错误

Php 使用REGEXP搜索三个字段并避免空子字符串错误,php,mysql,regex,Php,Mysql,Regex,我有一个搜索表单,用于搜索数据库字段“name”和“address”,还有一个下拉列表,用于搜索数据库字段“type”,并在search_results.php页面中显示结果。我的基本目标是搜索名称和地址字段,比如“dune”,并获得与其中包含“dune”的任何单词匹配的结果,例如搜索“dune”会返回“binedune”、“countdune”、“sanddune”等结果 另外,如果搜索返回空结果,特别是在使用下拉列表时,我不希望出现“empty substring”错误。例如,数据库中“ma

我有一个搜索表单,用于搜索数据库字段“name”和“address”,还有一个下拉列表,用于搜索数据库字段“type”,并在search_results.php页面中显示结果。我的基本目标是搜索名称和地址字段,比如“dune”,并获得与其中包含“dune”的任何单词匹配的结果,例如搜索“dune”会返回“binedune”、“countdune”、“sanddune”等结果

另外,如果搜索返回空结果,特别是在使用下拉列表时,我不希望出现“empty substring”错误。例如,数据库中“markers”表的“type”列包含字段“New York”、“Munich”、“London”和“Nairobi”。除了“新泽西”、“开罗”等其他城市外,这与下拉列表中显示的列表选项相同。当我选择例如“开罗”时,它在数据库中进行搜索,但没有找到开罗,并返回错误“Get error empty(sub)expression”

这就是我到目前为止所做的:

页面名称:search_results.php

<?PHP
//include db connection and functions file
require_once("lib/db.php");
require_once("lib/functions.php");

//Get POST vars from search form and drop down list

$name = "";
if(!empty($_POST['name'])){
    $name = mysql_real_escape_string(trim($_POST['name']));
}

if(!empty($_POST['address'])){
    $address = mysql_real_escape_string(trim($_POST['address']));
}

$type = "";
if(!empty($_POST['type'])){
    $type = mysql_real_escape_string(trim($_POST['type']));
}

//search form here. It redirects to this page, same as the drop down list which uses javascript to post vars to this same page

//This is the SQL query that is giving me the nightmares

$query="SELECT * FROM markers WHERE name REGEXP '^(".$name.")' OR address REGEXP '^(".$address.")' OR type REGEXP '^(".$type.")' AND active = 1";

$result=mysql_query($query) or die(mysql_error());
//display results here

你的问题不清楚。您需要提供示例输入和输出。我们无法猜测您的数据库中有哪些数据。希望这有帮助。为什么不喜欢为你工作?例:
。。。如果名称像“%${name}%”…
Dio F,LIKE语句将返回不需要的结果。我对问题进行了编辑,以包括示例输入和数据库中不匹配时出现的错误。