Search error statement

Discussion in 'PHP' started by venardv, Jun 9, 2009.

  1. #1
    How do I write an error statement if the search item wasn't found?
     
    venardv, Jun 9, 2009 IP
  2. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #2
    throw an exception
     
    gapz101, Jun 9, 2009 IP
  3. Social.Network

    Social.Network Member

    Messages:
    517
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    35
    #3
    I would not throw an exception! I will not go into exceptions, but this is bad advice. Below is one possible solution:

    $returned_rows = mysql_num_rows ($query);

    if ($returned_rows == 0){
    echo 'No records found';
    }else{
    // process rows here
    }
     
    Social.Network, Jun 9, 2009 IP
  4. venardv

    venardv Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    I have another code in there, which makes it even more confusing. I don't know how to order the codes.

    
    // connection
    mysql_select_db("//database"); 
    $search=$_GET["search"];
    if (isset($_GET['Submit']))
    {
    $result = mysql_query("SELECT * FROM Blog WHERE Title LIKE '%$search%'");
    while($r = mysql_fetch_array($result))
    {
    $title=$r["Title"];
    $text=$r["text"];
    $date=$r["date"];
    echo "$title $date $text";
    }
    }
    else
    {
    echo "// my blog content"
    }
    
    PHP:
     
    venardv, Jun 9, 2009 IP
  5. SHOwnsYou

    SHOwnsYou Peon

    Messages:
    209
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    mysql_select_db("//database"); 
    $search=$_GET["search"];
    if (isset($_GET['Submit']))
    {
    $result = mysql_query("SELECT * FROM Blog WHERE Title LIKE '%$search%'");
    $num = mysql_num_rows($result);
    if ($num == 0)
    {
    echo "No records found.";
    }
    else
    {
    while($r = mysql_fetch_array($result))
    {
    $title=$r["Title"];
    $text=$r["text"];
    $date=$r["date"];
    echo "$title $date $text";
    }
    }
    }
    else
    {
    echo "// my blog content"
    }
    
    PHP:
     
    SHOwnsYou, Jun 10, 2009 IP
  6. venardv

    venardv Member

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    Thanks again SHOwnsYou!
     
    venardv, Jun 11, 2009 IP