このスクリプトを実行しようとすると、このエラーが発生します。関数を呼び出す前に関数を作成しているので、関数が存在しないという事実を返す理由がわかりません。
エラー:
FUNCTION db.fn_distance_cosine が存在しません[]
また、同じスクリプトがローカルで正常に実行されていますが、別のマシンでこの問題が発生しています。
考え?
ありがとう!
<?php
$con = mysql_connect("localhost","username","password") or die ('Error connecting to mysql'); //remote
$db_selected = mysql_select_db('db', $con); //remote
if (!$db_selected) {
die ('Can\'t use selected database : ' . mysql_error());
}
$lat = $_GET["lat"];
$lon = $_GET["lon"];
$rows = array();
$establishFunction = mysql_query("DROP FUNCTION IF EXISTS fn_distance_cosine;
CREATE FUNCTION fn_distance_cosine (
latitude_1 DOUBLE,
longitude_1 DOUBLE,
latitude_2 DOUBLE,
longitude_2 DOUBLE
)
RETURNS DOUBLE
DETERMINISTIC
SQL SECURITY INVOKER
RETURN ACOS(
SIN(RADIANS(latitude_1)) * SIN(RADIANS(latitude_2))
+ COS(RADIANS(latitude_1)) * COS(RADIANS(latitude_2))
* COS(RADIANS(longitude_2 - longitude_1))
) * 3956.547;
");
mysql_query("SET @input_lat :=" . $lat . ";");
mysql_query("SET @input_lon :=" . $lon . ";");
mysql_query("SET @max_latitude := @input_lat + DEGREES(50.0/3956.547);");
mysql_query("SET @min_latitude := @input_lat - DEGREES(50.0/3956.547);");
mysql_query("SET @max_longitude := @input_lon + DEGREES(50.0/3956.547/COS(RADIANS(@input_lat)));");
mysql_query("SET @min_longitude := @input_lon - DEGREES(50.0/3956.547/COS(RADIANS(@input_lat)));");
if($result = mysql_query("SELECT
*,
fn_distance_cosine(lat, lon, @input_lat, @input_lon) AS distance_in_miles,
COUNT(*) AS store_number_count
FROM stores
JOIN items_stores
ON stores.store_number=items_stores.store_number
WHERE `lat` BETWEEN @min_latitude AND @max_latitude
AND `lon` BETWEEN @min_longitude AND @max_longitude
GROUP BY stores.store_code
HAVING store_code_count > 1
ORDER BY distance_in_miles
LIMIT 10;
")){
while ($r = mysql_fetch_assoc($result)){
$rows[] = $r;
}
}else{
//failed.
echo (mysql_error ());
}
print json_encode($rows);
mysql_close($con);
?>