SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if mysql select returns no results redirect

    pFirst time using a DB(MYSQL) and php to produce a little search.
    I have a list of zip codes for sales reps. The intial search worked great, but I want to have an if/else so if someone enters a zip code that is not in my database they are taken to a different page or recieve a message.

    Not haveing much luck.

    Here is the code I have:
    PHP Code:
    <?php
    mysql_connect 
    ("localhost""peterson_galiant","p3t3rs0n")  or die (mysql_error());  
    mysql_select_db ("peterson_zipcode"); 
    $term $_POST['term'];  
    $sql mysql_query("select * from reps_zip where zip like '%$term%'");  
    if (
    $row mysql_fetch_array($sql)){   
        echo 
    '<img src="'.$row['picture'].'" width=274>'
        echo 
    '<br/>'.$row['name'];  
        echo 
    '<br/> <a href="'.$row['page'].'">Contact Info</a>'
        echo 
    '<br/><br/>';  
    }  else {
        
    // the query executed, check if there are any rows in the result set
        
    if(mysql_num_rows($row) > 0){    echo '<br/>TEST<br/>';  
            
    // code to execute when there are any row(s) in the result set
        

    }  

    ?>
    Last edited by SpacePhoenix; Dec 16, 2010 at 01:16. Reason: placed php tags around the php code

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    num_rows should be executed on the RESULT. you're executing it on the ROW. (Hint: you'll have more luck replacing $row with $sql in your second if).

    You're using wildcard searching (LIKE) and then only pulling 1 result - are you intending on allowing partial matches, and if so, why are you only pulling one result?

    Try flipping the first IF around.

    IF num_rows == 0 {
    do your non-row stuff
    } ELSE {
    $row = fetch row
    do your row stuff
    }

  3. #3
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, flopped the two.
    First try at PHP and MYSQL, so some of what you mentioned about result and like lost me.

    I am not trying to pull partial results and I only want one results (though I cleaned up all duplicated from DB). So one zip code goes to a specific sales rep.

    Switching and changing to sql makes the word test show up, but for all searches even those that have data associated with them.

    PHP Code:
    <?php
    mysql_connect 
    ("localhost""peterson_galiant","p3t3rs0n")  or die (mysql_error());  
    mysql_select_db ("peterson_zipcode"); 
    $term $_POST['term'];  
    $sql mysql_query("select * from reps_zip where zip like '%$term%'");  
        if(
    mysql_num_rows($sql) > 0){    echo '<br/>TEST<br/>'

    }  else {if (
    $row mysql_fetch_array($sql)){   
        echo 
    '<img src="'.$row['picture'].'" width=274>'
        echo 
    '<br/>'.$row['name'];  
        echo 
    '<br/> <a href="'.$row['page'].'">Contact Info</a>'
        echo 
    '<br/><br/>';  

        } 
    }  

    ?>
    Last edited by SpacePhoenix; Dec 16, 2010 at 01:15. Reason: placed php tags around the php code

  4. #4
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, maybe I am asking this wrong.

    I have the search code working great if you search for a zip that is in the database it works fine.
    What I am trying to do is if the zip code searched for is not in the database I want to post a message or page that the search returned no results.

    Here is the code that works which doesn't include and option for no results:
    PHP Code:
    <?php
    mysql_connect 
    ("localhost""peterson_galiant","p3t3rs0n")  or die (mysql_error());  
    mysql_select_db ("peterson_zipcode"); 
    $term $_POST['term'];  
    $sql mysql_query("select * from reps_zip where zip like '%$term%'");  
    while (
    $row mysql_fetch_array($sql)){   
        echo 
    '<img src="'.$row['picture'].'" width=274>'
        echo 
    '<br/>'.$row['name'];  
        echo 
    '<br/> <a href="'.$row['page'].'">Contact Info</a>'
        echo 
    '<br/><br/>';  

    ?>
    Last edited by SpacePhoenix; Dec 16, 2010 at 01:14. Reason: placed php tags around the php code

  5. #5
    SitePoint Enthusiast LSC-Rob's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, NSW Australia
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mifand View Post
    PHP Code:
    <?php
    mysql_connect 
    ("localhost""peterson_galiant","p3t3rs0n")  or die (mysql_error());  
    mysql_select_db ("peterson_zipcode"); 
    $term $_POST['term'];  
    $sql mysql_query("select * from reps_zip where zip like '%$term%'");  
        if(
    mysql_num_rows($sql) > 0){    echo '<br/>TEST<br/>'

    }  else {if (
    $row mysql_fetch_array($sql)){   
        echo 
    '<img src="'.$row['picture'].'" width=274>'
        echo 
    '<br/>'.$row['name'];  
        echo 
    '<br/> <a href="'.$row['page'].'">Contact Info</a>'
        echo 
    '<br/><br/>';  

        } 
    }  

    ?>
    The correct format for the else/if is:
    PHP Code:
    } elseif { 
    EDIT:: * on further reading, the way you've done it, it would work either way...

    Also, your first IF checks for the number of rows being *more* than 0. If you want that piece of code to be executed when there are no results, you'll need:
    PHP Code:
    if(mysql_num_rows($sql) == 0){ 
    Rob G.
    ShopManager - Sales & Repair Business Management Software

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    You're on the right track...

    The second if is unnecessary - if there is a row (mysql_num_rows != 0), then $row = mysql_fetch_array($sql) will always be true.

    PHP Code:
    if(mysql_num_rows($sql) == 0) {
      
    //No Rows were Pulled. Either a missing zip code, or a bad query.
    } else {
      
    $row mysql_fetch_array($sql); //Will always work because there is at least 1 row.
      // Result found.


  7. #7
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Guys...
    Here is what I have that worked.
    HTML Code:
    <?php
    mysql_connect ("localhost", "peterson_galiant","p3t3rs0n")  or die (mysql_error());  
    mysql_select_db ("peterson_zipcode"); 
    $term = $_POST['term'];  
    $sql = mysql_query("select * from reps_zip where zip like '%$term%'");  
    $num = mysql_num_rows($sql);
    if($num == "0") {	
     //what to do if no results found 
     		echo '<br/>No Results Found<br/>';
     } else {	
     	while ($row = mysql_fetch_array($sql)){   		
     		echo '<img src="'.$row['picture'].'" width=274>'; 		
     		echo '<br/>'.$row['name'];  		
     		echo '<br/> <a href="'.$row['page'].'">Contact Info</a>'; 		
     		echo '<br/><br/>';  	
     } 
    } 
    ?>

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    if($num == "0") {
    mysql_num_rows() returns an integer, so there's no need for those quotes around the 0.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •