virtuexru Posted September 12, 2008 Share Posted September 12, 2008 I have a full text search. Basically I want to detect "No results" and echo a certain string. How should I go about doing this? This is the code I have currently: if(isset($_GET['hyb'])) { $keyword = mysql_real_escape_string($_GET['hyb']); $query = "SELECT * FROM ***** WHERE _subModel LIKE $keyword"; } if(isset($_GET['hc'])) { $keyword = mysql_real_escape_string($_GET['hc']); $query = "SELECT * FROM ***** WHERE _make LIKE \"%$keyword%\""; } if(!empty($keyword)) { $query = "SELECT *, MATCH(_make, _model, _desc, _year, _subModel) AGAINST('$keyword') AS score FROM ***** WHERE MATCH(_make, _model, _desc, _year, _subModel) AGAINST('$keyword') ORDER BY score DESC"; echo "<h3>Search Results for: <font color='red'>".$keyword."</font></h3>"; } if(empty($keyword)) { echo "<h3>Please enter a search term.</h3>"; $error = 1; } $result = mysql_query($query) or die(mysql_error()); if($error != 1) { while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $content = nl2br(strip_tags(substr($row['_desc'], 0, 250))); $content = $content; $score = $row['score']; if($score > 1.5) { echo " Results go here"; } etc.. Link to comment https://forums.phpfreaks.com/topic/123950-solved-how-to-detect-empty-sql-query-result/ Share on other sites More sharing options...
obsidian Posted September 12, 2008 Share Posted September 12, 2008 Check the number of records returned before you process the results: <?php $result = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { /* ... */ } } else { echo "No results"; } ?> Link to comment https://forums.phpfreaks.com/topic/123950-solved-how-to-detect-empty-sql-query-result/#findComment-639844 Share on other sites More sharing options...
virtuexru Posted September 12, 2008 Author Share Posted September 12, 2008 Perfect obsidian. Thank you. Link to comment https://forums.phpfreaks.com/topic/123950-solved-how-to-detect-empty-sql-query-result/#findComment-639853 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.