0

I want to know how do they do a query with only one textbox in the page, but the current text in the text box can be used as a parameter for the query. I'm currently using the query below but I don't think I'm getting the desired results, is there anything wrong with my query?

$result1=query_database("SELECT * FROM prod_table WHERE CATEGORY LIKE '$cuts%' OR PRODUCT LIKE '$cuts%' OR P_DESC LIKE '$cuts%' ", "onstor", $link);

?>


<?php

if(mysql_num_rows($result1)==0){

}else{

How to do this kind of query better.

3
  • 1
    What results are you getting vs what are you expecting? Also consider using binds since doing straight variable substation is very dangerous and buggy Commented Dec 10, 2010 at 13:45
  • What result do you get and what result do you expect to get? Commented Dec 10, 2010 at 13:45
  • I should get all the results beginning with 'a' when I type in 'a'. But I think I'm only getting one or two of them. Commented Dec 12, 2010 at 1:22

3 Answers 3

1

if query_database is not defined by you

mysql_query("SELECT * FROM prod_table WHERE CATEGORY LIKE '$cuts%' OR PRODUCT LIKE '$cuts%' OR P_DESC LIKE '$cuts%' ", "onstor", $link);
Sign up to request clarification or add additional context in comments.

1 Comment

I defined query_database on the connection file.
0

use % before $cut and escape this query is fine

 $result1=query_database("SELECT * FROM prod_table WHERE CATEGORY LIKE '%$cuts%' OR PRODUCT LIKE '%$cuts%' OR P_DESC LIKE '%$cuts%' ", "onstor", $link);

Comments

0

Add indexes for each of the 3 fields, CATEGORY,PRODUCT,P_DESC and it should run much faster.

Are you sure that LIKE '$cuts%' is correct? I believe that it should be LIKE '%$cuts%'. In the way that you have written it, it will only find the products which start with the value of the $cuts

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.