Jump to content

Echo Message When Results Are 0


Classico

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/270071-echo-message-when-results-are-0/
Share on other sites

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.

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. :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.