justlukeyou Posted May 20, 2012 Share Posted May 20, 2012 Hi, I am trying to write the code the read a search form however it is not reading my table 'productdbase' to return any results. I can echo from this table so I know it works but so far it just returns "Your search for 'Keyword' returned no results" as per below. Can anyone advise please. $results = "SELECT 'description', 'fulldescription' FROM 'productdbase' WHERE $where"; <?php if (isset($_POST['keywords'])){ $keywords = mysql_real_escape_string (htmlentities(trim($_POST['keywords']))); } $errors = array(); if (empty($keywords)) { $errors[] = 'Please enter a search term'; } else if (strlen($keywords)<3) { $errors[] = 'Your search must be three or more characters'; } else if (search_results($keywords) === false) { $errors[] = 'Your search for '.$keywords.' returned no results'; } if (empty($errors)) { search_results ($keywords); } else{ foreach($errors as $error) { echo $error, '</br>'; } } ?> <?php function search_results ($keywords) { $returned_results = array(); $where = ""; $keywords = preg_split('/[\s]+/', $keywords); $total_keywords = count($keywords); foreach($keywords as $key=>$keyword) { $where .= "'keywords' LIKE '%$keyword%'"; if ($key != ($total_keywords - 1)) { $where .= " AND "; } } $results = "SELECT 'description', 'fulldescription' FROM 'productdbase' WHERE $where"; $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0; if ($results_num === 0) { return false; }else{ echo 'something found.'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262819-search-code-not-reading-table/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 20, 2012 Share Posted May 20, 2012 Single-quotes go around string data values in sql statements, not around table and column names. Quote Link to comment https://forums.phpfreaks.com/topic/262819-search-code-not-reading-table/#findComment-1347010 Share on other sites More sharing options...
justlukeyou Posted May 20, 2012 Author Share Posted May 20, 2012 Thanks, I have tried changing lots areas without any luck. Is this area I need to change. I tried it without the single quotes, with double quotes and with no quotes. $results = "SELECT 'description', 'fulldescription' FROM 'productdbase' WHERE $where"; $results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0; Quote Link to comment https://forums.phpfreaks.com/topic/262819-search-code-not-reading-table/#findComment-1347035 Share on other sites More sharing options...
justlukeyou Posted May 20, 2012 Author Share Posted May 20, 2012 Oh so should be these ` instead of these '. I have changed these but it still doesnt read the field. It just echoes the error message. Quote Link to comment https://forums.phpfreaks.com/topic/262819-search-code-not-reading-table/#findComment-1347055 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.