on searching show message if not found any record!

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

  1. #1
    hi,

    i have a search page and it's working ok when records exist in database.
    <?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:
    but when the search is not locate any record i wan't to display a message "no records found". how i can do this?
     
    Kyriakos, Apr 17, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Use (under the query):

    
    if (mysql_num_rows($result) == 0){
        echo "No records found";
    }
    
    PHP:
    Jay
     
    jayshah, Apr 17, 2008 IP
  3. adwads.com

    adwads.com Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i am looking in your code and i cant understand this
    <?php if ($searchfor == "") {?>
    You must type a keyword!

    why are you closing php tag in almost every line?
     
    adwads.com, Apr 17, 2008 IP
  4. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    It's called inline HTML. It's not required, but quicker than using echo() or print() statements. Its faster than using the latter, hence why it's used for large(r) outputs.

    Jay
     
    jayshah, Apr 17, 2008 IP
  5. adwads.com

    adwads.com Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    i posted wrong example yeah i know that here he wrote html ;)
    but look this

    <?php } else { ?>
    <?php
     
    adwads.com, Apr 17, 2008 IP
  6. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #6
    Perhaps he wanted a line break?

    
    <?php
    for ($i=0; $i < 100000; $i++){
            rand(0, $i);
    }
    ?>
    <?php
    for ($i=0; $i < 100000; $i++){
            rand(0, $i);
    }
    ?>
    
    PHP:
    Times:
    
    real    0m0.202s
    user    0m0.050s
    sys     0m0.000s
    
    Code (markup):
    ----------------------------

    
    <?php
    for ($i=0; $i < 100000; $i++){
            rand(0, $i);
    }
    echo "\n";
    for ($i=0; $i < 100000; $i++){
            rand(0, $i);
    }
    ?>
    
    PHP:
    Times:
    
    real    0m0.203s
    user    0m0.060s
    sys     0m0.010s
    
    Code (markup):
    The include echo was actually slower on all accounts!

    The statistics don't lie! (Best time from 10 trials).

    Jay
     
    jayshah, Apr 17, 2008 IP
  7. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #7
    i'm using asp 2 years and the php last 2 weeks only. i'm learning now.
     
    Kyriakos, Apr 17, 2008 IP