2

I'm currently using jquery's ajax feature or whatever they call it. To load data from mysql database. Its working fine, but one of the built in features of this one is to load all the data which is on the database when you press on backspace and there's no character left on the text box.

Here's my query:

SELECT * FROM prod_table WHERE QTYHAND>0 AND PRODUCT LIKE '$prod%' OR P_DESC LIKE '$desc%' OR CATEGORY LIKE '$cat%'

As you can see I only want to load the products which has greater than 0 quantity on hand.

I'm using this code to communicate to the php file which has the query on it:

$('#inp').keyup(function(){


                var inpval=$('#inp').val();

            $.ajax({
                type: 'POST',
                data: ({p : inpval}),
          url: 'querys.php',
          success: function(data) {
            $('.result').html(data);

          }





        });

    });

Is it possible to also filter the data that it outputs so that when I press on backspace and there's no character left. The only products that's going to display are those with greater than 0 quantity?

1
  • can you make alert(data) in success block? Commented Jan 13, 2011 at 14:50

1 Answer 1

2

I think all you have to do is to change your query to:

SELECT * 
FROM prod_table 
WHERE QTYHAND>0 
AND (
    PRODUCT LIKE '$prod%' 
    OR P_DESC LIKE '$desc%' 
    OR CATEGORY LIKE '$cat%'
)

Not 100% sure if it is the same in SQL, but most often, AND has precedence over OR. So your original query would read like:

WHERE (... AND ...) OR ... OR ...

Now, if you have an empty string, then "something LIKE '%'" will always match and only one OR has to match to include the record in the result set.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.