我正在处理别人的代码而我不是PHP专家。我试图检索名称中包含撇号的记录。 SQL表包含撇号,如果我使用不包含撇号的名称的一部分,则返回搜索结果。
示例: 搜索莱利空调的商家名称只返回 Riley 或空调会返回莱利空调的预期结果。
以下是代码:
<?php
mysql_connect("localhost", "XXXXXX", "XXXXX") or die(mysql_error());
mysql_select_db("XXXXXX") or die(mysql_error());
//init vars
$category_selected="";
$terms_selected= "";
$alpha_selected="";
$searching=0;
$where="WHERE 1=1 ";
//get categories for dropdown
$query="(SELECT DISTINCT category1 FROM members) UNION (SELECT DISTINCT category2 FROM members) UNION (SELECT DISTINCT category3 FROM members) UNION (SELECT DISTINCT category4 FROM members) UNION (SELECT DISTINCT category5 FROM members )ORDER BY category1 ASC";
$categories = mysql_query($query);
//look for query string and build where clause
if(isset($_GET['category']) && $_GET['category']!=""){
$category_selected=$_GET['category'];
$where .= "AND (category1='$category_selected' OR category2='$category_selected' OR category3='$category_selected' OR category4='$category_selected' OR category5='$category_selected') ";
}
if(isset($_GET['terms']) && $_GET['terms']!=""){
$terms_selected=$_GET['terms'];
$where .= "AND (company LIKE '%$terms_selected%' OR category1 LIKE '%$terms_selected%') ";
}
if(isset($_GET['alpha']) && $_GET['alpha']!=""){
$alpha_selected=$_GET['alpha'];
if($alpha_selected=="#") {
$where .= "AND (company LIKE '1%' OR company LIKE '2%' OR company LIKE '2%' OR company LIKE '3%' OR company LIKE '4%' OR company LIKE '5%' OR company LIKE '6%' OR company LIKE '7%' OR company LIKE '8%' OR company LIKE '9%')";
}
else{
$where .= "AND company LIKE '$alpha_selected%'";
}
}
//get results if querystring is present
if ($category_selected!="" || $terms_selected!="" || $alpha_selected!=""){
$searching=1;
$query="SELECT * FROM members $where ORDER BY company ASC";
$results = mysql_query($query);
$num_rows = mysql_num_rows($results);
$terms_selected=stripslashes($terms_selected);
}
?>