因此,我有一个表单搜索公司列表,以查看该公司是否在网站上注册。PHP/MySQL搜索没有返回所有结果
当公司名称是三个或更少的字符,我可以看到它在数据库中的结果不会显示。
例如,如果公司的名称是“ABB优特”,检索是ABB,那么搜索如果搜索优特返回0
但是那么搜索返回结果。
下面是我使用的代码:
<?php
$out = '';
if ((isset($_POST['companysearch-name'])) && ($_POST['companysearch-name']==NULL)) {
$out .= '<h3>No matches. Please enter a company name.</h3>';
} else if (isset($_POST['companysearch-name'])) {
$queryString = htmlspecialchars($_POST['companysearch-name'],ENT_QUOTES);
if(strlen($queryString) >0) {
$query = $wpdb->get_results("SELECT DISTINCT (company_name) FROM wp_companies WHERE MATCH (company_name) AGAINST ('$queryString')");
$searchStr = "%" . $queryString . "%";
$searchStr = str_replace(" ","%",$searchStr);
$numresults = count($query);
if($query) {
$out .= '<div><ul>';
foreach($query as $result):
$out .= '<li>'.$result->company_name.'</li>';
endforeach;
$out .= '</ul></div>';
$out .= "<p>Is your company on this list? If YES, your company already has the scheme in place and you can setup a donation right now.</p>";
$out .= '<a class="button" href="/donate/"><span>Start Giving</span><b></b></a>';
} else {
$out .= '<h3>No matches found.</h3>';
$out .= "<p>Can't find your employer? They may have only just signed up - <a href='/contact-us/'>contact us</a> and we'll check it out for you.</p>";
}
}
}
?>
任何帮助极大的好评! ...温柔,这是我第一次。
你可以改变你的数据库的配置,也可以使用'那里COMPANY_NAME LIKE%$的queryString%'但很有可能会慢一些 – jcho360 2012-07-10 17:38:27