neogemima Posted May 7, 2009 Share Posted May 7, 2009 Hello, So I am querying my database with php to search by keyword in several different fields (i.e. someone can put in a word and it searches the title of the post, the content, location, and the company name for a match). As far as I can tell my query is correct, but it is returning false for some reason. I am pretty new to php, I am hoping I have just made a simple mistake. Here is my code: <?php //begin recent news doc $region=$_POST['region']; $query=$_POST['query']; $type=$_POST['type']; $query=trim($query); if(!get_magic_quotes_gpc()){ $region = addslashes($region); $query = addslashes($query); $type = addslashes($type); } @ $db = mysqli_connect(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); if (mysqli_connect_errno($db)) { echo 'Error: Could not connect to database. Please try again later.'; } mysqli_select_db($db, 'xxxxxxxxxx'); $sql = "SELECT id, date, type, region, city, company, title, content FROM Jobposting WHERE title like '%$query%' OR company like '%$query%' OR content like '%$query%' OR city like '%$query%' AND region = $region AND type = $type"; $queryresult = mysqli_query($db, $sql); $num_rows = mysqli_num_rows($queryresult); if ($num_rows > 0) { while ($rowresult = mysqli_fetch_array($queryresult, MYSQLI_ASSOC)){ $id = $rowresult['id']; echo '<a href="jobarticle.php?id='.$id.'">'.$rowresult['date'].' '.$rowresult['title'].' <b>'.$rowresult['company'].'</b> <font size="-1">'.$rowresult['region'].'</font></a>'; echo "<br>"; } }else{ echo '<font color="#999999">Your search returned no results.</font><br>'; echo ''.$query.','.$region.','.$type.''; } ?> Here, $query is a word they type in, and $region and $type are from drop down menus with values that match exactly the values that could be in their field in the table. Here is the error: "Search Results: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /home/content/b/i/o/biotechposting/html/pages/jobs/jobresults.php on line 67 Your search returned no results. biotech,Western Region,Diagnostics " I have it echo back the three variables on the last line to confirm they actually have a value. Is there something wrong with my query? Link to comment https://forums.phpfreaks.com/topic/157289-solved-php-search-query-returning-boolian-value/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 7, 2009 Share Posted May 7, 2009 Get php/mysql to tell you why the query failed by using the mysqli_error statement - http://php.net/mysqli_error Link to comment https://forums.phpfreaks.com/topic/157289-solved-php-search-query-returning-boolian-value/#findComment-829002 Share on other sites More sharing options...
neogemima Posted May 7, 2009 Author Share Posted May 7, 2009 Alright, this is what I expected: "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 'Region AND type = Entry Level' at line 1 " So it appears the issue is in my query, I don't see it though, what are some possible reasons it may return false? Just not matching the database? Should I use the "LIKE" statement for all three variables? Link to comment https://forums.phpfreaks.com/topic/157289-solved-php-search-query-returning-boolian-value/#findComment-829006 Share on other sites More sharing options...
PFMaBiSmAd Posted May 7, 2009 Share Posted May 7, 2009 String data must be enclosed in single-quotes so that it is not treated as a column name or a keyword. I'm guessing that where the variables $region and $type are at in the query, they are string data values? Link to comment https://forums.phpfreaks.com/topic/157289-solved-php-search-query-returning-boolian-value/#findComment-829010 Share on other sites More sharing options...
neogemima Posted May 7, 2009 Author Share Posted May 7, 2009 Yes, it's just some text like "Western Region" and "General Laboratory". Link to comment https://forums.phpfreaks.com/topic/157289-solved-php-search-query-returning-boolian-value/#findComment-829013 Share on other sites More sharing options...
neogemima Posted May 7, 2009 Author Share Posted May 7, 2009 Okay, I've gotten it to query the table now. The new issue that has arisen is that the query is accepting all rows that meet any of the three variable conditions rather than just the ones with the keyword AND the other two conditions met. Can I use brackets or anything to make sure that all queries returned meet both the region requirement and the type requirement? Link to comment https://forums.phpfreaks.com/topic/157289-solved-php-search-query-returning-boolian-value/#findComment-829018 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.