mdmartiny Posted January 3, 2012 Share Posted January 3, 2012 I am trying to write a script that will allow me visitors to my site to search my database for items. I followed a tutorial that I had found and tweaked it to fit my needs. I am not getting any results it keeps telling that there was no search results found. Even though I have the items that I have been testing on in my database. I have two files the main search.php file and the search_func.php file Here is the search.php file <form method='post' action='search.php'> <fieldset> <input type='text' name='search' id='search' value='type name here'/> <input type='submit' name='search_submit' id='search_submit' value='search'/> </fieldset> </form> <?php include('includes/search_func.php'); if(isset($_POST['search'])) { $keywords = mysql_real_escape_string(htmlentities(trim($_POST['search']))); $errors = array (); if (empty($keywords )){ $errors[] = 'Please enter a search term'; } else if (strlen($keywords ) < 3 ) { $errors[] = 'Search term must be more then three characters long'; } else if (search_results($keywords == false)) { $errors[] = 'Your search for ' .$keywords . ' showed no results'; } if (empty($errors)) { $results = search_results($keywords ); $results_num = count($results); $suffix = ($results_num != 1) ? 's' : ''; echo 'Your search for <strong>'. $keywords .'</strong> returned <strong>' .$results_num.'</strong> result'.$suffix.''; foreach($results as $result) { echo $result['first']; } } else { foreach($errors as $error){ echo $error; } } } ?> Here is the search_func.php file <?php include('includes/config.php'); connect(); function search_results($keywords ) { $returned = array(); $where = ""; $keywords = preg_split('/[\s+]/', $keywords ); $total = count($keywords ); foreach($keywords as $key=>$keyword) { $where .= "`l_name` LIKE '%$keyword%'"; if($key != ($total - 1)){ $where .= "OR"; } } $results = "SELECT `l_name`,`f_name`,`image`,`item_return` FROM `ttmautos` WHERE $where"; $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results): 0 ; if ($results_num === 0) { return false; } else { while($results_row = mysql_fetch_assoc($results)) { $returned_results[] = array( 'last' => $results_row['l_name'], 'first' => $results_row['f_name'], 'image' => $results_row['image'], 'item' => $results_row['item_return'] ); } return $returned_results; } } ?> If anyone could give me an idea of what I am doing wrong I would appreciate it. Link to comment https://forums.phpfreaks.com/topic/254255-not-getting-any-search-results/ Share on other sites More sharing options...
SergeiSS Posted January 3, 2012 Share Posted January 3, 2012 1. This code foreach($keywords as $key=>$keyword) { $where .= "`l_name` LIKE '%$keyword%'"; if($key != ($total - 1)){ $where .= "OR"; } could be changed to one string $where = implode( ' or ', $keywords ); 2. Why do you use here $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results): 0 ; one variable $results for different aims? You'd better use 2 different variables. 3. Echo your request just when it's created. And show it here - maybe it's wrong? Also you may try to start it in phpmyadmin, it's possible that you really don't have rows in a table that a satisfy your request. Link to comment https://forums.phpfreaks.com/topic/254255-not-getting-any-search-results/#findComment-1303613 Share on other sites More sharing options...
mdmartiny Posted January 3, 2012 Author Share Posted January 3, 2012 I think I may of found where the problem is in the code but not sure of what I am doing wrong. When I echo out the $results query. $where = ""; $keywords = preg_split('/[\s+]/', $keywords); $total = count($keywords ); foreach($keywords as $key=>$keyword) { $where .= "`l_name` LIKE '%$keyword%'"; if($key != ($total - 1)){ $where .= "OR"; } } $results = "SELECT * FROM `ttmautos` WHERE $where"; echo $results; This is what I am getting SELECT * FROM `ttmautos` WHERE `l_name` LIKE '%%' Link to comment https://forums.phpfreaks.com/topic/254255-not-getting-any-search-results/#findComment-1303831 Share on other sites More sharing options...
Pikachu2000 Posted January 3, 2012 Share Posted January 3, 2012 Start with a print_r() of the $keywords array to see what it contains. Link to comment https://forums.phpfreaks.com/topic/254255-not-getting-any-search-results/#findComment-1303833 Share on other sites More sharing options...
mdmartiny Posted January 3, 2012 Author Share Posted January 3, 2012 This is what I get when I do that SELECT * FROM `ttmautos` WHERE `l_name` LIKE '%%' Array ( [0] => ) Link to comment https://forums.phpfreaks.com/topic/254255-not-getting-any-search-results/#findComment-1303837 Share on other sites More sharing options...
PaulRyan Posted January 3, 2012 Share Posted January 3, 2012 To add to Pikachu comment, look at this line: } else if (search_results($keywords == false)) { Should be: } else if (!search_results($keywords)) { Regards, PaulRyan. Link to comment https://forums.phpfreaks.com/topic/254255-not-getting-any-search-results/#findComment-1303838 Share on other sites More sharing options...
mdmartiny Posted January 3, 2012 Author Share Posted January 3, 2012 It worked.. Once I changed this To add to Pikachu comment, look at this line: } else if (search_results($keywords == false)) { Should be: } else if (!search_results($keywords)) { Regards, PaulRyan. Everything worked fine. Thanks Paul Ryan Link to comment https://forums.phpfreaks.com/topic/254255-not-getting-any-search-results/#findComment-1303861 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.