searching mysql database. help.

Discussion in 'PHP' started by Kyriakos, Apr 17, 2008.

  1. #1
    hi everyone,

    i have this php code on my search.php file:
    <?php if ($searchfor == "") {?>
    You must type a keyword!
    <?php } else { ?>
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("storedb", $con);
    
    $result = mysql_query("SELECT * FROM products WHERE status=1 AND (product_id LIKE '".$searchfor."' OR name LIKE '".$searchfor."' OR manufacturer LIKE '".$searchfor."') ORDER BY pricer");
    ?>
    <html table for results>
    <?php } ?>
    
    PHP:
    when i type the exactly product_id, name or manufacturer the search is ok. but when i type a section of the product_id, name or manufacturer it appears nothing. this thing in asp it's working but not in php.

    sorry for my english.
     
    Kyriakos, Apr 17, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    product_id LIKE '%$searchfor%'

    The same for the other search fields.
    If you don't use the % wild cards, the LIKE will act just as a = (equal).
     
    CreativeClans, Apr 17, 2008 IP
  3. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    look below
     
    Kyriakos, Apr 17, 2008 IP
  4. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #4
    <?php if ($searchfor == "") {?>
    You must type a keyword!
    <?php } else { ?>
    <?php
    $con = mysql_connect("localhost","root","");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("storedb", $con);
    
    $result = mysql_query("SELECT * FROM products WHERE status=1 AND (product_id LIKE '%".$searchfor."%' OR name LIKE '%".$searchfor."%' OR manufacturer LIKE '%".$searchfor."%') ORDER BY pricer");
    ?>
    <html table for results>
    <?php } ?>
    PHP:
    I hope it will help.
     
    coderbari, Apr 17, 2008 IP
  5. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    i have another question:

    i want the page to display a message like that "no records found" if no records found with this keywords. can anyone give some help?
     
    Kyriakos, Apr 17, 2008 IP
  6. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0