Don E Posted March 21, 2012 Share Posted March 21, 2012 I'm having some trouble querying the database for searching. The website is created object oriented. Below is the search.php page that takes the search query/word/phrase. Below that is the member function that is suppose to return the content based on the search word or phrase. This all use to work fine until I upgraded PHP. Everything below is basically a prototype, so if there's anything that can be improved, I understand. What's happening is.. I am not getting the error message I set up when nothing matches. However, when I use a keyword that I know will return results, it works. I just don't understand why the error message doesn't return anymore when nothing is found. search.php: <?phprequire_once("_includes/listings_page.php");require_once("_includes/search_pagination.php");//Content is also included here.$searchPage = new listings();$searchPage->__set("title", "Search Results | Nucul Performance");$searchPage->intro_content = "<p>Search results...</p>";$searchPhrase = trim($_GET['s']);$searchPhraseClean = str_replace(",", "",$searchPhrase);// this is for inserting search keywords, ip address and userAgent(browser) into database.(To get an idea what people are searching for; thus supply demand)if(!empty($searchPhraseClean)){$mysqli = new MySQLi("xx","xx","xx","xx") or die("No connection");$mysqli->query("INSERT INTO searchKeywords (keywords, ip_address, browser_type) VALUES ('$searchPhraseClean','$_SERVER[REMOTE_ADDR]','$_SERVER[HTTP_USER_AGENT]')");$mysqli->close();}// making new instance of the class searchPagination.$searchContent = new searchPagination();if(!empty($searchPhraseClean)) {$pageLinks = $searchContent->getPageLinks($searchPhraseClean);//returns pagination links$searchPage->links = $pageLinks;$getContent = $searchContent->getContent($searchPhraseClean);//returns an array with content or returns a non-array with a message.if(is_array($getContent))$searchPage->content = $getContent;else$searchPage->message = $getContent;$searchPage->Display();}else{$searchPage->message = "<p class=\"error\">Please specify a search phrase or keyword.</p>";$searchPage->Display();}?> Below is: $getContent = $searchContent->getContent() member function from class searchPagination();. public function getContent($search){ $mysqli = new MySQLi("xx","xx","xx","xx") or die("No connection"); $searchClean = $mysqli->real_escape_string($search); $this->results = $mysqli->query("SELECT * FROM postings WHERE MATCH(title,post) AGAINST ('$searchClean') LIMIT $this->start, $this->per_page"); // first search query if($this->results == FALSE) // if first search query gets no results, try second below { // Noticed: I am pretty sure the program gets to this point but it's as if the below is not being ran.. $this->results = $mysqli->query("SELECT * FROM postings WHERE title LIKE '%$searchClean%' OR post LIKE '%$searchClean%' LIMIT $this->start, $this->per_page "); // second search if($this->results == FALSE) // if second fails, nothing found display error below { return "<p class=\"error\">No results for: <em>$searchClean</em></p><p>Please try again. Be more specific, using whole words.</p>"; // Noticed: This is what I want to return when nothing is found in the database based on the search keyword/phrase } } while ($row = $this->results->fetch_assoc())//displays results { $id = $row['id']; $title = $row['title']; $posted_on = $row['posted_on']; $posted_by = $row['posted_by']; $post = $row['post']; $picture = $row['picture']; $price = $row['price']; $filesize = filesize(np_UPLOADPATH.$picture); if($filesize > np_MAXFILESIZE) { $picSize = getimagesize(np_UPLOADPATH.$picture); $resizeImage = new resizeImage(); $resizedPic = $resizeImage->imageResize($picSize[0],$picSize[1], 400); // below is an array. The while loop loops through the array setting each element(array key) with whatever is being drawn from the database. Then I used a foreach to output the array in the contents DIV.$this->content[] = ("<h2>$title</h2><h4>$posted_on posted by $posted_by</h4><div class=\"topArrow\"></div><div class=\"contentContainer\"><table width=\"100%\" border=\"0\" bgcolor=\"\"><tr><td>$post</td></tr><tr><td colspan=\"2\" align=\"center\"><img src=\"/np/photos/$picture\" $resizedPic/></td></tr><tr><td align=\"center\"colspan=\"2\"><hr/>$price | Inquiry | <a href=\"/np/viewphotos/".$id."/".$newTitle=strtolower(str_replace(array(": ", " ","!","'","?"),"-",$title))."/\">See more pictures</a></td></tr></table></div>"); } else {$this->content[] = ("<h2>$title</h2><h4>$posted_on posted by $posted_by</h4><div class=\"topArrow\"></div><div class=\"contentContainer\"><table width=\"100%\" border=\"0\" bgcolor=\"\"><tr><td>$post</td></tr><tr><td colspan=\"2\" align=\"center\"><img src=\"/np/photos/$picture\"/></td></tr><tr><td align=\"center\"colspan=\"2\"><hr/>$price | Inquiry | <a href=\"/np/viewphotos/".$id."/".$newTitle=strtolower(str_replace(array(": ", " ","!","'","?"),"-",$title))."/\">See more pictures</a></td></tr></table></div>"); } } $mysqli->close(); return $this->content;} Apologies if anything is confusing. Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 21, 2012 Share Posted March 21, 2012 The return value from a query that doesn't return any results is not false, it is a result set with 0 records. You can use the num_rows property of the result to figure out how many results there are. It only returns false if there was an error with the query. Link to comment Share on other sites More sharing options...
Don E Posted March 21, 2012 Author Share Posted March 21, 2012 JSG, I switched switched if($this->results == FALSE) to if($this->results->num_rows == 0) and it works now but I'm getting this error:Notice: Trying to get property of non-objectThanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 21, 2012 Share Posted March 21, 2012 The error is on that line? It still may be false if the query returned an error, so you may want to check for false first and show the error message from MySQL, then check the result. Link to comment Share on other sites More sharing options...
Don E Posted March 22, 2012 Author Share Posted March 22, 2012 JSG, This is the error message I'm getting from MySQL after doing what you suggested: Can't find FULLTEXT index matching the column list Link to comment Share on other sites More sharing options...
justsomeguy Posted March 22, 2012 Share Posted March 22, 2012 You need to have a fulltext index on any set of columns that you're using MATCH to search. Link to comment Share on other sites More sharing options...
Don E Posted March 22, 2012 Author Share Posted March 22, 2012 Column 'post' didn't have FULLTEXT set for it. I just set it to FULLTEXT but I am still getting that error.. is it because of my query(s)? "SELECT * FROM postings WHERE MATCH(title,post) AGAINST ('$searchClean') LIMIT $this->start, $this->per_page" Since only columns 'title' and 'post' have FULLTEXT set for them, should I change the query above to instead:"SELECT title, post FROM postings WHERE MATCH(title,post) AGAINST ('$searchClean') LIMIT $this->start, $this->per_page" ? Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 22, 2012 Share Posted March 22, 2012 You need a single fulltext index that covers both columns, or a query that searches each column individually. Link to comment Share on other sites More sharing options...
Don E Posted March 22, 2012 Author Share Posted March 22, 2012 After altering the table: ALTER TABLE postings ADD FULLTEXT(title, post)... has fixed the problem. Thanks JSG. Much appreciated. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.