Prev | Current Page 240 | Next

Marc Delisle

"Mastering phpMyAdmin 2.11 for Effective MySQL Management"


phpMyAdmin's treatment for functions is in many points similar to what we have
covered for procedures:
The query box to input a function
The use of a delimiter
The mechanism to manipulate an already-defined function
??? ??? ???
MySQL 5.0 Features
[ 274 ]
Let's define a function that retrieves the country name, based on its code. I prefer to
use a param_ prefix to clearly identify parameters inside the function's definition and
a var_ prefix for local variables.
CREATE FUNCTION get_country_name(param_country_code CHAR(2))
RETURNS VARCHAR(50)
BEGIN
DECLARE var_country_name VARCHAR(50) DEFAULT 'not found';
SELECT description
INTO var_country_name
FROM country
WHERE code = param_country_code;
RETURN var_country_name;
END
We note that our newly-created function can be seen on the database's Structure
page, along with its friend the add_page procedure:
Testing the Function
To test this function, we can try:
SELECT CONCAT('ca->', get_country_name('ca'), ', zz->', get_country_
name('zz')) as test;
Exporting Stored Procedures and Functions
When exporting a database, procedures and functions do not appear (by default) in
an SQL export. A checkbox Add CREATE PROCEDURE / FUNCTION exists in the
Structure dialog of the Export page, to include those in the SQL export file. If we try
it (with a minimum phpMyAdmin version of 2.


Pages:
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252