Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extract the values of the vars in a url
/**
Si le pasamos una cadena como:
'index.php?var=17&otravar=12&masvar=33&muchovar=21'
nos devuelve una tabla con los valores de las variables
*/
DROP PROCEDURE IF EXISTS extractVars;
CREATE PROCEDURE extractVars(url VARCHAR(255))
BEGIN
DECLARE posicion INT;
DECLARE fin INT;
DECLARE valor VARCHAR(255);
DROP TEMPORARY TABLE IF EXISTS vars;
CREATE TEMPORARY TABLE vars(
id INT NOT NULL AUTO_INCREMENT,
var CHAR(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE = Memory CHARSET = utf8;
SET posicion := LOCATE('=', url);
WHILE posicion > 0 DO
SET url := SUBSTR(url FROM posicion + 1);
SET fin := LOCATE('&', url);
IF fin > 0 THEN
SET valor := SUBSTR(url FROM 1 FOR fin - 1);
ELSE
SET valor := SUBSTR(url FROM 1);
END IF;
SET posicion := LOCATE('=', url);
INSERT INTO vars(var) VALUES (valor);
END WHILE;
SELECT * FROM vars;
DROP TEMPORARY TABLE vars;
END;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment