Jump to content

question about stored functions...


scarhand

Recommended Posts

i have this code:

 

DELIMITER $$

CREATE FUNCTION LAT_LNG_DISTANCE(lat1 FLOAT, lng1 FLOAT, lat2 FLOAT, lng2 FLOAT) RETURNS int(11)
BEGIN
  DECLARE latD FLOAT;
  DECLARE lngD FLOAT;
  DECLARE latS FLOAT;
  DECLARE lngS FLOAT;
  DECLARE a FLOAT;
  DECLARE c FLOAT;

  SET lat1 = RADIANS(lat1);
  SET lng1 = RADIANS(lng1);
  SET lat2 = RADIANS(lat2);
  SET lng2 = RADIANS(lng2);

  SET latD = lat2 - lat1;
  SET lngD = lng2 - lng1;

  SET latS = SIN(latD / 2);
  SET lngS = SIN(lngD / 2);

  SET a = POW(latS, 2) + COS(lat1) * COS(lat2) * POW(lngS, 2);
  SET c = 2 * ASIN(LEAST(1, SQRT(a)));

  RETURN ROUND(c * 6378137);
END;
$$
DELIMITER ;

 

first off, do i have to run this query every time i want to use the function?

 

second off, when im run this query in phpmyadmin i get the error: 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 'DELIMITER' at line 1

 

Link to comment
https://forums.phpfreaks.com/topic/176400-question-about-stored-functions/
Share on other sites

1. No. You just need to create function once and then it is... well... stored in the database to be used at will.

2. No idea why it happens... seems fine to me... Only thing that comes to my mind is to check whether you got all necessary privileges.

 

[edit]

 

Which MySQL version do you use?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.