Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Why does my search function always yield no results?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Why does my search function always yield no results?

Postby the5thace » Mon Oct 29, 2012 2:28 pm

Hello, just going along with a tutorial and trying to get a search engine to work on my database!
I have no idea where I have gone wrong and there are no errors in the script but every time I enter something in to the search engine box it always yields no results and it still gives me the array number.



Search page:
Code: Select all
 <?php
     include 'searchengine/func.inc.php';
     ?>
      <h2>Search for Channels</h2>
      <form action="" method="POST">
      <p>
     <input type="text" name="keywords" /> <input type="submit" value ="Search" />
     </p>
      </form>
      <?php
     if (isset($_POST['keywords'])) {
        $keywords = mysql_real_escape_string(htmlentities(trim($_POST['keywords'])));
        $errors = array();
       
        if (empty($keywords)) {
           $errors[] = 'Please enter search terms';
        } else if (strlen($keywords)<3) {
           $errors[] = 'Your search term must be 3 or more characters';
        } else if (search_results($keywords) === false) {
           $errors[] = 'Your search returned no results';
        }
        if (empty($errors)) {
          search_results($keywords);
        } else {
           foreach($errors as $error) {
              echo $error, '</br>';
           }
        }
     }
    
     ?>


Search function:
Code: Select all
<?php
include 'db.inc.php';
function search_results($keywords) {
   $returned_results = array();
   $where = "";
   
   $keywords = preg_split('/[\s]+/', $keywords);
   print_r($keywords);
   $total_keywords = count($keywords);
   
   foreach($keywords as $key=>$keyword) {
      $where .= "`keywords` LIKE '%$keywords%'";
      if ($key != ($total_keywords - 1)) {
         $where .= " AND ";
      }
   }
   $results = "SELECT `chanelname`, LEFT(`description`, 70) as `description`, `Channeltype`, `game1`, `game2`, `keywords` FROM `Userchannel` WHERE $where";
   $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;
     if($results_num === 0) {
       return false;
    } else {
       echo 'Something found.';
}
}
?>


Connection to db:

Code: Select all
<?php
mysql_connect('184.173.202.130', 'theace', '**********');
mysql_select_db('theace_LetsPlays');
?>


The database name shows up as "_LetsPlays" in the drop down menu in php my admin and I'm using a table called "Userchannel"

I hope someone can help me out!
the5thace
New php-forum User
New php-forum User
 
Posts: 1
Joined: Mon Oct 29, 2012 2:18 pm

Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.