Skip to content

Instantly share code, notes, and snippets.

@sbarrat
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbarrat/432386784462a11d8fe0 to your computer and use it in GitHub Desktop.
Save sbarrat/432386784462a11d8fe0 to your computer and use it in GitHub Desktop.
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