I have function to cal miles distance:
DELIMITER //
DROP FUNCTION IF EXISTS distance_miles//
CREATE FUNCTION distance_miles(lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT)
RETURNS FLOAT(120)
BEGIN
DECLARE pi, q1, q2, q3 FLOAT;
DECLARE rads FLOAT DEFAULT 0;
SET pi = PI();
SET lat1 = lat1 * pi / 180;
SET lon1 = lon1 * pi / 180;
SET lat2 = lat2 * pi / 180;
SET lon2 = lon2 * pi / 180;
SET q1 = COS(lon1-lon2);
SET q2 = COS(lat1-lat2);
SET q3 = COS(lat1+lat2);
SET rads = ACOS( 0.5*((1.0+q1)*q2 - (1.0-q1)*q3) );
RETURN 6378.388 * rads * 0.621371192;
END//
DELIMITER ;
I run this as 1 query from phpmyadmin. It run's Ok. But i dont see this in routines table in information_schema.
Also, when am trying run this from php i got an error
$mdb->query($mdb->mes("
DELIMITER //
DROP FUNCTION IF EXISTS distance_miles//
CREATE FUNCTION distance_miles(lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT)
RETURNS FLOAT(120)
BEGIN
DECLARE pi, q1, q2, q3 FLOAT;
DECLARE rads FLOAT DEFAULT 0;
SET pi = PI();
SET lat1 = lat1 * pi / 180;
SET lon1 = lon1 * pi / 180;
SET lat2 = lat2 * pi / 180;
SET lon2 = lon2 * pi / 180;
SET q1 = COS(lon1-lon2);
SET q2 = COS(lat1-lat2);
SET q3 = COS(lat1+lat2);
SET rads = ACOS( 0.5*((1.0+q1)*q2 - (1.0-q1)*q3) );
RETURN 6378.388 * rads * 0.621371192;
END//
DELIMITER ;
"));
this one:
error:1064
CALLED:[Resource id #8]***You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\r\nDELIMITER ~~ ' at line 1 in db query: [\r\nDELIMITER ~~Pls help me, what happens? Completely dont understood this.
- How to look if function exists?
- It will appear in information_shema table ROUTINES?
- why is problem with php running this query?
====
SELECT ROUTINE_TYPE, ROUTINE_NAME FROM INFORMATION_SCHEMA.ROUTINES
Also - dont show this function ... strange this all...
Also this: SHOW CREATE FUNCTION distance_miles
Answers: MySQL said: Documentation
1305 - FUNCTION distance_miles does not exist开发者_C百科
...
mysql_query
doesn't support multiqueries. See manual.
You just need to remove the DELIMITER
lines, split the queries by yourself, and the CREATE FUNCTION
should work.
$mdb->query($mdb->mes("DROP FUNCTION IF EXISTS distance_miles"));
$mdb->query($mdb->mes("CREATE FUNCTION distance_miles(lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT)
RETURNS FLOAT(120)
BEGIN
DECLARE pi, q1, q2, q3 FLOAT;
DECLARE rads FLOAT DEFAULT 0;
SET pi = PI();
SET lat1 = lat1 * pi / 180;
SET lon1 = lon1 * pi / 180;
SET lat2 = lat2 * pi / 180;
SET lon2 = lon2 * pi / 180;
SET q1 = COS(lon1-lon2);
SET q2 = COS(lat1-lat2);
SET q3 = COS(lat1+lat2);
SET rads = ACOS( 0.5*((1.0+q1)*q2 - (1.0-q1)*q3) );
RETURN 6378.388 * rads * 0.621371192;
END
"));
$mdb->query($mdb->mes("DROP FUNCTION IF EXISTS distance_miles"));
$mdb->query($mdb->mes("delimiter $
CREATE FUNCTION distance_miles(lat1 FLOAT, lon1 FLOAT, lat2 FLOAT, lon2 FLOAT)
RETURNS FLOAT(120)
DETERMINISTIC BEGIN
DECLARE pi, q1, q2, q3 FLOAT;
DECLARE rads FLOAT DEFAULT 0;
SET pi = PI();
SET lat1 = lat1 * pi / 180;
SET lon1 = lon1 * pi / 180;
SET lat2 = lat2 * pi / 180;
SET lon2 = lon2 * pi / 180;
SET q1 = COS(lon1-lon2);
SET q2 = COS(lat1-lat2);
SET q3 = COS(lat1+lat2);
SET rads = ACOS( 0.5*((1.0+q1)*q2 - (1.0-q1)*q3) );
RETURN 6378.388 * rads * 0.621371192;
END $
"));
精彩评论