1

I am having a problem with getting this query to search my database successfully.

I am wanting to query the database to show anything that includes the search in their name.

Currently it is working. However it will only search through the column of description, it will not search through the others.

I am really stumped as to why this is I have the OR command in the query.

The other issue I am having is when I add two-digit-sic into the query, (which is one of my database columns) It returns this error:

Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'two' in 'where clause'' in /home/content/57/9632657/html/client/prospects/wp-content/themes/prospects/page-sic-code-search.php:67 Stack trace: #0 /home/content/57/9632657/html/client/prospects/wp-content/themes/prospects/page-sic-code-search.php(67): PDOStatement->execute() #1 /home/content/57/9632657/html/client/prospects/wp-includes/template-loader.php(74): include('/home/content/5...') #2 /home/content/57/9632657/html/client/prospects/wp-blog-header.php(19): require_once('/home/content/5...') #3 /home/content/57/9632657/html/client/prospects/index.php(17): require('/home/content/5...') #4 {main} thrown in /home/content/57/9632657/html/client/prospects/wp-content/themes/prospects/page-sic-code-search.php on line 67

<?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="POST" 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>






 $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 '%$search%' OR description-2 LIKE '%$search%'");
$query->bindValue(1, "%$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>";
        }
    }






?>







<!-- 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(); ?>
AbsoluteƵERØ
  • 7,816
  • 2
  • 24
  • 35
  • This will help. I know Fred marked your question as duplicate rather vaguely. What he's trying to say is that you are handling your PDO parameters wrong. Check out this for PDO examples: http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – AbsoluteƵERØ Aug 31 '17 at 05:44
  • While I've nominated this for reopening, the actual answer to your specific question is here: https://stackoverflow.com/questions/11117134/implement-like-query-in-pdo – AbsoluteƵERØ Aug 31 '17 at 05:46
  • Thank you so much! I am going to give it a shot and I will comment the results – chris lawson Aug 31 '17 at 17:50

0 Answers0