Classico Posted October 30, 2012 Share Posted October 30, 2012 Hi, I'm having a small problem with my query. I'm basically getting the user's input, and searching the database for a match towards their input. If it finds a match, display it. If it doesn't, display a message. My query does display the data if it finds a match, but it display everything from the database if it doesn't find anything. Could someone tell me what I'm doing wrong please? Here is my code: <?php $search = mysql_real_escape_string(trim($_POST['ticket'])); $query = mysql_query("SELECT * FROM unbanappeal WHERE ticket_id LIKE '%$search%'"); $ticket_id_check = mysql_num_rows($query); if($ticket_id_check < 0){ echo "No results found."; }else{ while($row = mysql_fetch_assoc($query)){ echo $row['username']; } } ?> Also, is it possible for me to change my query from displaying any data that's like the user's input, to the data that is exactly like the user's input? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted October 30, 2012 Share Posted October 30, 2012 // if($ticket_id_check < 0){ should be if($ticket_id_check <= 0){ as for getting an exact match, change the LIKE in the SQL to an = sign and remove all % wildcards from the string decleration. Quote Link to comment Share on other sites More sharing options...
White_Lily Posted October 30, 2012 Share Posted October 30, 2012 since you can't really have less then 0 results, try; if($variable == 0){ //failed }else{ //passed } Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 30, 2012 Share Posted October 30, 2012 (edited) The code you've provided will return all records where the $search value is contained in the ticket_id field. The only reason I can see where all results would be returned is if $search is empty. Since you say that when you enter a value and there is a match that the appropriate records are displayed I assume that you are referencing the correct post value. If you are entering a value and there is no match - then no records would be displayed. So, I believe, either that is not the correct code that you've posted or the results you are getting are not exactly as you've stated them. Edited October 30, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
Classico Posted October 30, 2012 Author Share Posted October 30, 2012 The 'No results found' problem has been fixed. Thanks guys. The code you've provided will return all records where the $search value is contained in the ticket_id field. The only reason I can see where all results would be returned is if $search is empty. Since you say that when you enter a value and there is a match that the appropriate records are displayed I assume that you are referencing the correct post value. If you are entering a value and there is no match - then no records would be displayed. So, I believe, either that is not the correct code that you've posted or the results you are getting are not exactly as you've stated them. I need to check whether the user has entered their value in the input field next, as I'm doing 'No results found' as a default message if a user decides to go to that page in the URL. It does work now though, thanks for your reply. Quote Link to comment Share on other sites More sharing options...
White_Lily Posted October 30, 2012 Share Posted October 30, 2012 To check if a value is present, just simply do this: if(empty($variable)){echo "The field is empty, enter a value.";} Quote Link to comment Share on other sites More sharing options...
lovephp Posted October 30, 2012 Share Posted October 30, 2012 if(empty($ticket_id_check)){ echo "No results found."; }else{ while($row = mysql_fetch_assoc($query)){ echo $row['username']; } } 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.