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

Edited by Psycho
Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.