質問

I have an empty column and I would like to update rows with value of PHP function

<?php
function dystans1($lat1, $lon1, $lat2, $lon2) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$km = $dist * 60 * 1.1515*1.609344;
$unit = strtoupper($unit);
return $km;      
}

$connection = @mysql_connect('localhost', '***', '***')

or die('Brak połączenia z serwerem MySQL.<br />Błąd: '.mysql_error());

$db = @mysql_select_db('****', $connection)

or die('Nie mogę połączyć się z bazą danych<br />Błąd: '.mysql_error()); 




$wynik = mysql_query("SELECT * FROM field_data_field_adres")
or die('Błąd zapytania');

if(mysql_num_rows($wynik) > 0) {
while($r = mysql_fetch_assoc($wynik)) {

    $km = dystans1($r['field_adres_lat'], $r['field_adres_lng'], 52.40633, 16.91811);
    echo $km;
    mysql_query("UPDATE field_data_field_adres SET dystans = $km ");



}
}

?> 

Script inserts into dystans the same value. I would like to do that every row has another value

役に立ちましたか?

解決

Your update function is missing a WHERE clause. Meaning that for every row in the loop, it calculates the distance and then updates all rows to the same value. You need to add a WHERE clause that uses something like an ID column when updating the table so it will update just the one row and not all rows.

Example:

mysql_query("UPDATE field_data_field_adres SET dystans = $km WHERE id={$r['id']}");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top