我正在创建一个SIC代码查找,我的查询有一个奇怪的问题。我有6个表名,描述,描述-2,描述-3。和两位数的,四位数的,六位数的,我得到了意想不到的结果。
如果我在任何描述表(例如:农业)中搜索项目,它将返回正确的查询。它会显示它应该显示。以及如果我只搜索一个两位数的sic。 (例如:01)
但是,如果我尝试通过六位数字或四位数字搜索它将不会显示记录。
奇怪的是,当我搜索正确的六位数时,它会说它找到了记录,但却没有显示它们。如果你输入一个不正确的sic代码,它只会说找不到记录。工作六位数字(011198)非工作SIC(112112)的例子
我真的不知道为什么会这样。
这是现场直播:http://mainstaycomputing.com/client/prospects/list-brokers-resources/sic-code-search/
<?php
get_header();
?>
<div id="main-content" style = 'position: relative'>
<div class = 'wrapper'>
<h1 class="entry-title main_title"><?php the_title(); ?></h1>
<div class = 'sic-text'>
<p> SIC codes are assigned by the Government and are standard at the 4 digit level.
The 8 digit codes may be customized by each individual list owner. Because we represent all list
sources, there may be variance in what the 8 digit codes represent. For greatest accuracy,
when speaking with one of our List Brokers please supply the sic code # along with a description so
we can provide as exact a match as possible.To use this search, simply type the Industry you’re
looking for into the Search By Keyword field. For instance, entering “Dentists” will cause all
businesses related to dentists listed. <! — If you know the SIC code and want to know the industry
name, enter the 8 digit code into the Search By Code field. –> </p>
</div>
<form action="" method="GET" class = 'sic-search'>
<input class = 'sic-search-text' type="text" name="search" placeholder="Search for an industry, eg 'Agriculture'"/>
<input type="submit" value="Search" class = 'sic-search-button'/>
</form>
<?php
$search = $_POST['search'];
$host = "****";
$user = "****";
$password = "****";
$database_name = "****";
$min_length = 2;
// you can set minimum length of the sic if you want
if(strlen($search) >= $min_length && $search != ''){ // if sic length is more or equal minimum length then
echo "<p id='rowCount'> </p>";
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
// Search from MySQL database table
$search=$_POST['search'];
$query = $pdo->prepare("select * from siccodes where description LIKE ? OR description-2 LIKE ?");
$query->bindValue(2, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
if (!$query->rowCount() == 0) {
echo "Search found :<br/>";
echo "<table class='sic-code-table'";
echo "<tr><th>description</th><th>two-digit-sic</th><th>description</th><th>four-digit-sic</th><th>description</th><th>six-digit-sic</th></tr>";
while ($results = $query->fetch()) {
echo "<tr><td>";
echo $results['description'];
echo "</td><td>";
echo $results['two-digit-sic'];
echo "</td><td>";
echo $results['description-2'];
echo "</td><td>";
echo $results['four-digit-sic'];
echo "</td><td>";
echo $results['description-3'];
echo "</td><td>";
echo $results['six-digit-sic'];
echo "</tr>";
}
echo "</table>";
} else {
echo "<p style = 'text-align:center; margin-bottom:30px; color: red;'>No results match" . " '" . $search . "' " . "Please try another search term.</p>";
}
} else {
if (!empty($_POST['search'])) {
echo "<p style = 'text-align:center; margin-bottom:30px; color: red;'> Please search again, '$search' is to short.</p>";
}
}
$host = "****";
$user = "***";
$password = "***!";
$database_name = "*****";
mysql_connect("****", "****", "****") or die("Error connecting to database: ".mysql_error());
/*
localhost - it's location of the mysql server, usually localhost
root - your username
third is your password
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("****") or die(mysql_error());
/* tutorial_search is the name of database we've created */
?>
<?php
$query = $_GET['search'];
// gets value sent over search form
// if the query is null which it would be when they first enter the page then show all results
if(empty($_GET['search'])) {
// $query = '01';
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM siccodes ") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text `
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
echo "<table class='sic-code-table'";
echo "<tr><th>description</th><th>two-digit-sic</th><th>description</th><th>four-digit-sic</th><th>description</th><th>six-digit-sic</th></tr>";
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<tr><td>";
echo $results['description'];
echo "</td><td>";
echo $results['two-digit-sic'];
echo "</td><td>";
echo $results['description-2'];
echo "</td><td>";
echo $results['four-digit-sic'];
echo "</td><td>";
echo $results['description-3'];
echo "</td><td>";
echo $results['six-digit-sic'];
echo "</tr>";
// posts results gotten from database(title and text) you can also show id ($results['description'])
}
echo "</table>";
}
} // end of displaying all results
/* this is the actual search */
$min_length = 2;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM siccodes
WHERE (`description` LIKE '%".$query."%') OR (`description-2` LIKE '%".$query."%') OR (`description-3` LIKE '%".$query."%') OR (`two-digit-sic` = '$query') OR (`four-digit-sic` = '$query') OR (`six-digit-sic` = '$query')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text `
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
$raw_num_rows = mysql_num_rows($raw_results);
$num_rows = $raw_num_rows - 1;
echo "<p id='rowCount'> We have retrieved " . " " . $num_rows . " " . " records related to the keyword '$query'</p>";
echo "<table class='sic-code-table'";
echo "<tr><th>description</th><th>two-digit-sic</th><th>description</th><th>four-digit-sic</th><th>description</th><th>six-digit-sic</th></tr>";
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<tr><td>";
echo $results['description'];
echo "</td><td>";
echo $results['two-digit-sic'];
echo "</td><td>";
echo $results['description-2'];
echo "</td><td>";
echo $results['four-digit-sic'];
echo "</td><td>";
echo $results['description-3'];
echo "</td><td>";
echo $results['six-digit-sic'];
echo "</tr>";
// posts results gotten from database(title and text) you can also show id ($results['description'])
}
echo "</table>";
}
else{ // if there is no matching rows do following
echo "<p style='text-align:center;color:red;'>No results for '$query' | Please try again";
}
}
else{ // if query length is less than minimum
echo "<p style='color:orange; text-align:center; '>Minimum length is ".$min_length."</p>";
}
?>
<!-- creates a form at the bottom of the list if there are more than 100 records -->
<?php if(mysql_num_rows($raw_results) > 100) : ?>
<form action="" method="GET" class = 'sic-search'>
<input class = 'sic-search-text' type="text" name="sic" placeholder="Search for an industry, eg 'Agriculture'"/>
<input type="submit" value="Search" class = 'sic-search-button'/>
</form>
<?php endif; ?>
</div> <!-- end of wrapper -->
</script>
</div> <!-- #main-content -->
<?php get_footer(); ?>