PHP: Search properly
<p>I have this right now:</p>

<pre><code>if(isset($_POST["search"]) &amp;&amp; !empty($_POST["search"])) {
    $full_name = mysql_real_escape_string($_POST["search"]);
    $sex = mysql_real_escape_string($_POST["sex"]);
    list($firstname, $lastname) = array_map('ucfirst', explode(' ', $full_name));
    $query = "SELECT firstname, lastname, id, user_name, sex, last_access, bostadsort FROM users WHERE (firstname LIKE '$firstname' OR lastname LIKE '$lastname') AND sex = '$sex'"; 
    $result1 = mysql_query($query) or die(mysql_error());
    $count = mysql_num_rows($result1);
    while($get = mysql_fetch_array  ($result1)){
        echo $get["firstname"] . " " .$get["lastname"]."&lt;br&gt;";
    }
}
</code></pre>

<p>This is my search query. Now the form is called "Search for full names". You type in, and then it splits to $firstname , $lastname.</p>

<p>Works great, no problems. </p>

<p>Although if you ONLY enter a user´s LASTNAME, because maybe you dont remember the users firstname, then this code will take it as a firstname, because all before a space is firstname (list() line), and puts it in $firstname and will result nothing, as there's no firstname with that lastname.</p>

<p>How can i solve this? If you search by firstname it works fine, full name too, but not only lastname. Any smart solution?</p>
