Can't collect data from database the correct way
<p>I have the function <code>getDistance()</code>. The function <code>findDistance()</code> inside the while loop, calculates the distance between 2 users, by using coordinates (latitude-longitude), and returns to var <code>$djson</code> the distance in meters. <code>$distance</code> is a string committed by the user for first time and <code>$user_old["distance"]</code> is a string which is called from a database in <code>$query</code>. I wanted to be able in <code>$matched_names</code>, to save all the names of the users from my database, for who the condition inside <code>if()</code> is true, regarding the sum of the distance of the new user who commits his data  and the old ones inside the database. The problem is that <code>$matched_names</code> saves the first name which is called from the database and for as many times the loop goes on without even considering the <code>if()</code> restriction. For example if the first name called in <code>$user</code> is "Mike", and <code>$user</code> has 5 rows then the output will be: <code>Mike,Mike,Mike,Mike,Mike</code>.</p>

<p>I suppose that i have made some mistake in the way things work inside while..   </p>

<pre><code>&lt;?php
public function getDistance($uuid, $name, $distance, $latstart, $lonstart, $latend, $lonend, $gcm_regId) {

    $query = sprintf("SELECT uid, gcm_regid, name, distance,latstart, lonstart, latend, lonend  FROM user_demand WHERE latstart='%s' AND lonstart='%s'",
    mysql_real_escape_string($latstart),
    mysql_real_escape_string($lonstart));
    $user = mysql_query($query);

    $no_of_rows = mysql_num_rows($user);
    $user_old = mysql_fetch_assoc($user);

    while( $user_old = mysql_fetch_assoc($user)) {
        $djson = $this-&gt;findDistance($latend,$lonend,$user_old["latend"],$user_old["lonend"] );
        if ($user_old["distance"] + $distance &gt;= $djson) {
            $match = $this -&gt;df-&gt;addUserMatch($user_old['gcm_regid'],$user_old['name'],$gcm_regId,$name);
            $matched_names = array_fill(0,$no_of_rows,$user_old['name']);   
            $matched_gcmz = array_fill(0,$no_of_rows,$user_old['gcm_regid']); 
        }
    }

    $registatoin_ids = array($gcm_regId);

    $message = array("names" =&gt;$matched_names,"gcm" =&gt; $matched_gcmz);
    $result = $this -&gt;gcm-&gt;send_notification($registatoin_ids, $message);
}
?&gt;
</code></pre>
