MYSQL Functions

Stored functions are similar to stored procedures: they are named program units that contain one or more MySQL statements.
They differ from procedures in the following ways:

  • The parameter list of a function may contain only IN parameters. OUT and INOUT parameters are not allowed. Specifying the IN keyword is neither required nor allowed.
  • The function itself must return a single value, whose type is defined in the header of the function.
  • Functions can be called from within SQL statements.
  • A function may not return a result set.
Syntex:

CREATE FUNCTION functionname (parametrs)
RETURNS type /* type->{STRING|INTEGER|REAL|DECIMAL} */
BEGIN
.......
RETURN value /* If u want to return*/
END;

Example :

CREATE FUNCTION get_volume(radius FLOAT)
RETURNS FLOAT DETERMINISTIC
BEGIN
DECLARE volume FLOAT;
SET volume = (4/5)*PI()*POW(radius,2);
RETURN volume;
END

Php :
// Connection
mysql_query($link,SELECT get_volume( 5.5));

In Command prompt >SELECT get_volume( 5.5)

No comments: