PHP Search only returning one result

Soldato
Joined
28 Sep 2008
Posts
14,158
Location
Britain
Hi all,

You can forget my previous CSS Table result issue, I'm trying something cleaner now, but its only returning one result.

Here's the php

PHP:
<?php
    $query = $_GET['query']; 
    // gets value sent over search form
     
    $min_length = 3;
    // you can set minimum length of the query if you want
     
    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
         
        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;
         
        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection
         
        $raw_results = mysql_query("SELECT * FROM contacts WHERE (`name` LIKE '%".$query."%') OR (`email` LIKE '%".$query."%') OR (`telephone` LIKE '%".$query."%') OR (`address` LIKE '%".$query."%')") or die(mysql_error());
             
           if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
             
            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the 
					$id = stripslashes($results['id']);
					$name = stripslashes($results['name']);
					$telephone = stripslashes($results['telephone']);
					$email = stripslashes($results['email']);
					$address = stripslashes($results['address']);
             
                //echo "<p><h3>".$results['name']."</h3>".$results['address']."</p>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }
             
        }
        else{ // if there is no matching rows do following
            echo "No results";
        }
         
    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }
?>

<p><strong>Name:</strong><?php echo $name; ?></p>
<p><strong>Telephone:</strong><?php echo $telephone; ?></p>
<p><strong>Email:</strong><?php echo $email; ?></p>
<p><strong>Address:</strong><?php echo $address; ?></p>

I believe the issue is around this particular area:

PHP:
            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the 
					$id = stripslashes($results['id']);
					$name = stripslashes($results['name']);
					$telephone = stripslashes($results['telephone']);
					$email = stripslashes($results['email']);
					$address = stripslashes($results['address']);
             
                //echo "<p><h3>".$results['name']."</h3>".$results['address']."</p>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])

I commented out the "echo <p><h3>" line above. Actually, if I leave that in, it works, but doesn't look pretty...

Can anyone see what I might have broken?

Ta :)
 
Soldato
OP
Joined
28 Sep 2008
Posts
14,158
Location
Britain
Cool, this works. Thanks. Don't worry, this is just helping out a friend.

However, can you shed some light on this?

I've used a similar code before, and that returned all results from the database (not those based on query) using this code, and it worked:

PHP:
<?php

//This is the query for the administration; 
//we are obtaining all articles that are available with all columns.
$query = 'SELECT * FROM contacts ORDER BY id DESC';

//This runs the query to view
//If we get a positive result in $results, it will process the while loop
If ($results = mysql_query ($query)) {

//Creates the display table
}
?>

<p>Back To:&nbsp;<a href="admin.php">Administration</a> </p>
<p>Export all contacts from within the database to a CSV file</p>

<form action="csv.php">
<input type="submit" value="Download Now">
</form>
<br />
<br />



<p><strong>Delete Contacts from the database here</strong></p>

<?php

While ($row = mysql_fetch_array($results)) { 
//inputs the data into the table
		$id = stripslashes($row['id']);
		$name = stripslashes($row['name']);
		$telephone = stripslashes($row['telephone']);
		$email = stripslashes($row['email']);
		$address = stripslashes($row['address']);


?>
	
<p><strong>Name:</strong><?php echo $name; ?></p>
<p><strong>Telephone:</strong><?php echo $telephone; ?></p>
<p><strong>Email:</strong><?php echo $email; ?></p>
<p><strong>Address:</strong><?php echo $address; ?></p>
				
	
                <form action="dc.php" method="post">
                <input type="hidden" name="id" value="<?=$id ?>">
                <input type="submit" name="submit" value="Delete">
                </form><br /><br />
<p><img src="images/separator.gif" width="525" height="1" alt="seperator"/></p>

		
	<?php
	}
	?>
	
   
<?php
mysql_close();
?>
 
Back
Top Bottom