justlukeyou Posted May 21, 2012 Share Posted May 21, 2012 Hi, I have written some code however when I echo the query it runs it twice... SELECT `description`, `fulldescription` FROM `productdbase` WHERE `keywords` LIKE '%table%'something found.SELECT `description`, `fulldescription` FROM `productdbase` WHERE `keywords` LIKE '%table%'something found. I only have the query once in my code. Any suggesions? <?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 "; } } $query_string = "SELECT `description`, `fulldescription` FROM `productdbase` WHERE $where"; echo $query_string; $query = mysql_query($query_string); if ($results_num === 0) { return false; }else{ echo 'something found.'; } } ?> Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted May 21, 2012 Share Posted May 21, 2012 Where in the code do you actually call the search_results() function? Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Hi, Not sure what you mean. Quote Link to comment Share on other sites More sharing options...
PeoMachine Posted May 21, 2012 Share Posted May 21, 2012 You're calling the search_funtion twice. When you use it in "else if (search_results($keywords) === false)", it echoes too. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Should it be doing that? The problem I have is that when I enter something that is in my database it tells me no records are found. Is this because it is running the query twice or just echoing it twice? Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 If I remove that part it doesn't show anything! Quote Link to comment Share on other sites More sharing options...
PeoMachine Posted May 21, 2012 Share Posted May 21, 2012 You're testing the var "$results_num" to know if you have any record, but this var is not receiving any value... You need to fetch the result to know if you have any record. You just need to know if you have records in the database? Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Hi, I have the records in the database. Im a bit lost as to what you are suggesting. At this stage it should say 'something found.' if it is in my database. But its not doing that. Quote Link to comment Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 It's easy .... See comments. <?php if (isset($_POST['keywords'])){ $keywords = mysql_real_escape_string (htmlentities(trim($_POST['keywords']))); } $errors = array(); // the results variable $results = null; 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 (($results = search_results($keywords)) === false) { // get the results $errors[] = 'Your search for '.$keywords.' returned no results'; } // show errors if any if (!empty($errors)) { foreach($errors as $error) { echo $error, '</br>'; } } Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Many thanks, I entered in those three points and it returns with the same result. If run the query instead of echoing it still doesn't read my database. For example I know I have table in database. Your search for table returned no results Is there anyway I echo errors out which tell me why it is not reading the db? Quote Link to comment Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 Where are you using the $results variable? Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Im not, all the code I have is what you see. Does that display the final results? The problem I have is that it always says that nothing is found in my DB. Quote Link to comment Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 $results will have the info you want. You just need to use it. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Thanks, do I need to use it differently to how I am so far? I cant understand why nothing is being found. Quote Link to comment Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 How are you confirming that "nothing" is being found? Show the code. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Hi, Its this part here $errors[] = 'Your search for '.$keywords.' returned no results'; Its not reading the database so it tells me that no results are returned. How can I check if it is reading the dbase? Quote Link to comment Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 Ok. Could you verify that the SQL does return something? What do you see when you run that directly in MySQL? And please show your updated code. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Hi, I ran the following: "SELECT `description`, `fulldescription` FROM `productdbase` WHERE table" And this is the response I got.. #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"SELECT `description`, `fulldescription` FROM `productdbase` WHERE table"' at line 1 Im not familiar with running queries directly in mysql, how do I search a keyword if I am posting it from another? Quote Link to comment Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 Run the same query that you used PHP to echo out. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 Hi, This is it $query_string = "SELECT `description`, `fulldescription` FROM `productdbase` WHERE $where"; This is the result: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$query_string = "SELECT `description`, `fulldescription` FROM `productdbase` WH' at line 1 Does it mean anything to you? Quote Link to comment Share on other sites More sharing options...
Kays Posted May 21, 2012 Share Posted May 21, 2012 No. You had a echo $query_string; after that $query_string and before the actual call to mysql_query. Then copy and past that here. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 21, 2012 Author Share Posted May 21, 2012 This is what I had. Do you mean this part? This is the query I have run. $query_string = "SELECT `description`, `fulldescription` FROM `productdbase` WHERE $where"; echo $query_string; $query = mysql_query($query_string); Quote Link to comment Share on other sites More sharing options...
Kays Posted May 22, 2012 Share Posted May 22, 2012 Right. So when you run that PHP script, it should print something out right? More specifically, it should output the value of $query_string. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted May 22, 2012 Author Share Posted May 22, 2012 Hi, This is the result I got from running the following query: $query_string = "SELECT `description`, `fulldescription` FROM productdbase WHERE $where"; QUERY: $query_string = "SELECT `description`, `fulldescription` FROM 'productdbase' WHERE $where"; echo $query_string; $query = mysql_query($query_string); Quote Link to comment Share on other sites More sharing options...
Kays Posted May 22, 2012 Share Posted May 22, 2012 Are you lost? I said run the PHP script. Not show me the lines of code again. Yes, I saw them back 10 posts. That is so far back in history. Quote Link to comment 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.