searching a database through PHP

Discussion in 'PHP' started by Rahul Bose, Jun 29, 2010.

  1. #1
    i'm trying to create a page where i can search my database for items. The field that is being searched on my database is the name of the item in there.

    So say i have various items with the word computer or close to it, i want to be able to type it in on a form field and pres the submit button which will query it to search the database for any row that has the word computer in its name.


    I've been trying the following code, but its not fully working. What i mean is that it brings results but only if its spelled exactly.

    I've tried using wildcards before, after and in between the variable, but i haven't had any luck.

    Hope someone can help me out
     
    Rahul Bose, Jun 29, 2010 IP
  2. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    You get any error? and your code with wildcards in

    $result = mysql_escape_query($_GET['key']);
    $category = mysql_escape_query($_GET['cat']);
    $get_items = "SELECT * FROM tablename WHERE category='$category' AND i_name LIKE '%$result%'";
    PHP:
     
    themullet, Jun 29, 2010 IP
  3. imperialDirectory

    imperialDirectory Peon

    Messages:
    395
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Its hard to tell where the problem is. Can you give exact table's name and some sample data? Also, try adding `` to wrap table name and column names to see if it works.

    
    $get_items = "SELECT * FROM `tablename` WHERE `category`='$category' AND `i_name` LIKE '$result'";
    
    Code (markup):
    I doubt it makes any difference but worth a short
     
    imperialDirectory, Jun 29, 2010 IP
  4. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #4
    dont forget to add wildchars unless you're searching for exact matches...

    $get_items = "SELECT * FROM `tablename` WHERE `category`='$category' AND `i_name` LIKE '%$result%'";
     
    creativeGenius, Jun 29, 2010 IP
  5. vincent.fedorchak

    vincent.fedorchak Guest

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Just a novice opinion, but don't you need to include the concatenation operator in your string, like so:

    $get_items = "SELECT * FROM `tablename` WHERE `category`=' . $category . ' AND `i_name` LIKE '%$result%'";

    Just a thought.
     
    vincent.fedorchak, Jun 29, 2010 IP
  6. AnubisTheJackle

    AnubisTheJackle Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Well, in this instance no because in PHP when you use double quotes (") instead of single quotes (') then variables are read correctly. You CAN use it, but you need to end the quote. So in the case you've supplied it would be:

    $get_items = "SELECT * FROM `tablename` WHERE `category`='" . $category ." ' AND `i_name` LIKE '%$result%'";
    PHP:
    But this could also be written as

    $get_items = 'SELECT * FROM `tablename` WHERE `category`=\'' . $category .'\' AND `i_name` LIKE \'%'.$result.'%\'';
    PHP:
     
    AnubisTheJackle, Jun 29, 2010 IP