i have a problem with my search page

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • simon2x1
    New Member
    • Dec 2008
    • 123

    i have a problem with my search page

    i have two simon in a field in my database called Fname and simon
    in a field called Lname when i search for simon it will only display
    one of the simon

    Code:
    <?php
    	
    	$link = mysql_connect( "localhost", "user2", "point" );
    	if ( ! $link )
    	die( "Couldn't connect to MySQL" );
    	mysql_select_db( "files", $link )
    	or die ( "Couldn't open $database: ".mysql_error() );
    	
    	if (isset($_POST["term"])){
    	$term = $_POST["term"];
    	//echo "$term";
    	
    	$result = mysql_query("SELECT * FROM user WHERE Fname like '%$term%' OR Lname like '%$term%'",$link);
    	$myrow = mysql_fetch_array($result);
    	
    	$Fname = $myrow["Fname"];
    	$Lname = $myrow["Lname"];
    	$Address = $myrow["Address"];
    	$Phone = $myrow["Phone no"];
    	$Email = $myrow["Email"];
    	
    	
    	echo "$Fname<br>";
    	echo "$Lname<br>";
    	echo "$Address<br>";
    	echo "$Phone<br>";
    	echo "$Email<br>";
    	}else {
    	
    	echo "search not found";
    	}
    ?>
  • dumm
    New Member
    • Dec 2008
    • 10

    #2
    Should be:

    Code:
    <?php
     
        $link = mysql_connect( "localhost", "user2", "point" );
        if ( ! $link )
        die( "Couldn't connect to MySQL" );
        mysql_select_db( "files", $link )
        or die ( "Couldn't open $database: ".mysql_error() );
     
        if (isset($_POST["term"])){
        $term = $_POST["term"];
        //echo "$term";
     
        $result = mysql_query("SELECT * FROM user WHERE Fname like '%$term%' OR Lname like '%$term%'",$link);
    	
        if(mysql_num_rows($result) > 0){
    
        while($myrow = mysql_fetch_assoc($result))
        {
     
        $Fname = $myrow["Fname"];
        $Lname = $myrow["Lname"];
        $Address = $myrow["Address"];
        $Phone = $myrow["Phone no"];
        $Email = $myrow["Email"];
     
     
        echo "$Fname<br>";
        echo "$Lname<br>";
        echo "$Address<br>";
        echo "$Phone<br>";
        echo "$Email<br>";
        
        } // end while    
    
        }else { // if no results found
     
        echo "search not found";
        }
    ?>
    Right now you only fetch the first entry found.

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      You need to use mysql_fetch_arr ay() to return an array of the results obtained from the database. You can then loop over this array to get all results.

      Comment

      Working...