Jump to content


This topic is now archived and is closed to further replies.


php ORDER BY help

Recommended Posts

Hi all

I have a search form on my website which lets you search for a buddy, the search can be proformed as a name search using there first name, for example


The form then goes to the php page and does a query on what has been typed in, it then sorts the results by the persons surname, for example

David Adams
David Holmes
David Waferry

The query I currently perform is

$usql = "SELECT * FROM users WHERE `firstname` = '$fname' ORDER BY surname ASC";
$ushow = @mysql_query($usql,$connection) or die(mysql_error());

while ($urows = mysql_fetch_array($ushow)) {

print $urows['firstname'];
print " ";
print $urows['surname'];
print "<br><br>";


What I want to now do is to offer a surname search box on the same form, what should happen when someone sdoes a search under firstname and surname it would return back all results that match JUST the firstname (like it currently does). What I want to do though, it to show the people who matched the surname at the top of the results and then show just the people who matched the firstname below.

The firstname input box on the search form is called "firstname" and the surname one which I want to add will be called "surname".

Is this at all possible?

Loads of thanks in advance


Share this post

Link to post
Share on other sites
[code]SELECT firstname, lastname, IF(lastname='$lastname' , 0 ,1) as seq
FROM users
WHERE firstname='$firstname' OR lastname='$lastname'
ORDER BY seq, lastname, firstname[/code]

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.