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
Joined
3 Jun 2005
Posts
3,119
Location
The South
As Dj_Jestar mentioned, you're not displaying individual results within the loop rather you're looping around the results, setting the variables but only displaying the last result due to your HTML tags being outside the loop.

Change the while loop to something like the below -
Code:
while($results = mysql_fetch_array($raw_results)){ 
	// Set Variables
	$id = stripslashes($results['id']); 
	$name = stripslashes($results['name']); 
	$telephone = stripslashes($results['telephone']); 
	$email = stripslashes($results['email']); 
	$address = stripslashes($results['address']);
	
	// Display Result
	echo("<p><strong>Name:</strong>{$name}</p>"); 
	echo("<p><strong>Telephone:</strong>{$telephone}</p>"); 
	echo("<p><strong>Email:</strong>{$email}</p>");
	echo("<p><strong>Address:</strong>{$address}</p>");
}

And ideally you should be using PDO for database access, or at least MySQLi, rather than the vanilla MySQL PHP functions and a similarly you want to be using prepared statements. Plenty of tutorials around for using both - Nettus+ has some create guides.
I also recommend getting a decent book on PHP OOP and MVC and start getting into that mindset if you plan to do this as a career.
 
Last edited:
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();
?>
 
Soldato
Joined
18 Oct 2003
Posts
19,413
Location
Midlands
Look at your while loop. So where the brace starts here:

While ($row = mysql_fetch_array($results)) {

And finishes here

<?php
}
?>


In between is looping, so all of the outputs (echo statements) are in the right place.

As a point of ref on the basics of PHP and getting up to speed faster: http://www.tizag.com/phpT/
 
Soldato
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
Why aren;t you using the mysqli_* set of functions? mysql_* are very depreciated. Have a read of http://stackoverflow.com/questions/...-and-will-be-removed-in-the-future-use-mysqli

Maybe because the wealth of guides/tutorials on the web are still using the old mysql functions, as are numerous PHP scripts and libraries.

Not that i disagree, i love using PDO myself, but it's a bit of headache to use syntax wise unless you've used it quite extensively already and have a pretty good handle on writing PHP. Of course, there are numerous frameworks to aid in making the transition easier, but they're only as simple and straight-forward as your knowledge and know-how of PHP allows.
 
Back
Top Bottom