2011-08-09 76 views
10

有沒有辦法執行一個MySQL查詢,並有直接urldecode輸出中的一列,而不是讓PHP做。MySQL選擇與URL解碼

例如,這臺 '聯繫人' 會包含

------------------------------------ 
|name  |email     | 
------------------------------------ 
|John Smith|johnsmith%40hotmail.com| 
------------------------------------
SELECT * FROM `contacts` 

將輸出,

約翰·史密斯| johnsmith%[email protected]

有沿着線的東西,

SELECT name, urldecode(email) FROM `contacts` 

要輸出,

約翰·史密斯| [email protected]

+0

你只使用PHP打印HTML /文本時需要解碼,所以...沒必要打擾 – ajreal

+4

這將是更好的不保存url編碼在第一位:) –

+0

真的嗎?我將JQuery Autocomplete插件與遠程數據源一起使用,返回的查詢必須至少有2列,值和ID。所以SELECT CONCAT(name,'',email)AS VALUE,ID FROM'contacts'是當前填充自動完成列表 – Rob

回答

0

我設法使用單獨的PHP代碼行來將查詢響應從SQL語句中解回,然後將其傳遞給用於JQuery自動完成的json_encode。

7

我覺得它重要,回答原來的問題(不考慮替代品),主要是因爲其被要求,是一個有效的問題:

簡短的回答:你不能。

對於解碼URL編碼的字符串,沒有本機SQL函數。

長的答案:你需要寫一個自定義的SQL函數:

編碼/解碼MySQL的函數:http://www.dzone.com/snippets/urlencodeurldecode-mysql

最好的答案:任何URL編碼可以存儲爲UTF8。

通過以下操作將表格更改爲UTF8:將「latin1」替換爲「utf8」,然後導入。然後運行一個腳本(僅一次),例如PHP,Python或Rails來解碼列並將其存回。現在您已準備好按照預期使用您的數據庫。沒有開銷。

Python代碼做到這一點:

import urllib 

import MySQLdb 

def decode(): 

    con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="root", passwd="", db="hostip") 
    cursor = con.cursor() 

    cursor.execute("SELECT city, name FROM cityByCountry") 

    for row in cursor.fetchall(): 
    print row 
    cursor.execute("UPDATE cityByCountry SET name=%s WHERE city=%s", (urllib.unquote(row[1]), row[0])) 

if __name__ == "__main__": 
    decode() 
17

如果有人還在尋找一個MySQL解碼功能:

DROP TABLE IF EXISTS urlcodemap; 

CREATE TABLE `urlcodemap` (
    `id` INT(11) NOT NULL AUTO_INCREMENT, 
    `encoded` VARCHAR(128) NOT NULL, 
    `decoded` VARCHAR(128) NOT NULL, 
    UNIQUE KEY urlcodemapUIdx1(encoded), 
    PRIMARY KEY (`id`) 
) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 

-- Table that contains the list of encode\decode. 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%20"," "); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%21","!"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%22",""""); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%23","#"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%24","$"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%25","%"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%26","&"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%27","'"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%28","("); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%29",")"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%2A","*"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%2B","+"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%2C",","); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%2D","-"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%2E","."); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%2F","/"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%30","0"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%31","1"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%32","2"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%33","3"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%34","4"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%35","5"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%36","6"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%37","7"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%38","8"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%39","9"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%3A",":"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%3B",";"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%3C","<"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%3D","="); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%3E",">"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%3F","?"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%40","@"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%41","A"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%42","B"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%43","C"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%44","D"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%45","E"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%46","F"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%47","G"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%48","H"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%49","I"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%4A","J"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%4B","K"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%4C","L"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%4D","M"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%4E","N"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%4F","O"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%50","P"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%51","Q"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%52","R"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%53","S"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%54","T"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%55","U"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%56","V"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%57","W"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%58","X"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%59","Y"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%5A","Z"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%5B","["); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%5C","\\"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%5D","]"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%5E","^"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%5F","_"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%60","`"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%61","a"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%62","b"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%63","c"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%64","d"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%65","e"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%66","f"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%67","g"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%68","h"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%69","i"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%6A","j"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%6B","k"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%6C","l"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%6D","m"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%6E","n"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%6F","o"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%70","p"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%71","q"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%72","r"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%73","s"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%74","t"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%75","u"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%76","v"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%77","w"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%78","x"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%79","y"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%7A","z"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%7B","{"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%7C","|"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%7D","}"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%7E","~"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%80","`"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%82","‚"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%83","ƒ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%84","„"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%85","…"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%86","†"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%87","‡"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%88","ˆ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%89","‰"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%8A","Š"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%8B","‹"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%8C","Œ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%8E","Ž"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%91","‘"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%92","’"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%93","「"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%94","」"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%95","•"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%96","–"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%97","—"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%98","˜"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%99","™"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%9A","š"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%9B","›"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%9C","œ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%9E","ž"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%9F","Ÿ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%A1","¡"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%A2","¢"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%A3","£"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%A4","¤"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%A5","¥"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%A6","¦"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%A7","§"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%A8","¨"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%A9","©"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%AA","ª"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%AB","«"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%AC","¬"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%AE","®"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%AF","¯"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%B0","°"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%B1","±"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%B2","²"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%B3","³"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%B4","´"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%B5","µ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%B6","¶"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%B7","·"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%B8","¸"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%B9","¹"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%BA","º"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%BB","»"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%BC","¼"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%BD","½"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%BE","¾"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%BF","¿"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%C0","À"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%C1","Á"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%C2","Â"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%C3","Ã"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%C4","Ä"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%C5","Å"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%C6","Æ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%C7","Ç"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%C8","È"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%C9","É"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%CA",""); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%CB","Ë"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%CC","Ì"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%CD","Í"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%CE","Î"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%CF","Ï"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%D0","Ð"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%D1","Ñ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%D2","Ò"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%D3","Ó"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%D4","Ô"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%D5","Õ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%D6","Ö"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%D7","×"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%D8","Ø"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%D9","Ù"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%DA","Ú"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%DB","Û"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%DC","Ü"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%DD","Ý"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%DE","Þ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%DF","ß"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%E0","à"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%E1","á"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%E2","â"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%E3","ã"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%E4","ä"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%E5","å"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%E6","æ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%E7","ç"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%E8","è"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%E9","é"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%EA","ê"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%EB","ë"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%EC","ì"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%ED","í"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%EE","î"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%EF","ï"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%F0","ð"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%F1","ñ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%F2","ò"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%F3","ó"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%F4","ô"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%F5","õ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%F6","ö"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%F7","÷"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%F8","ø"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%F9","ù"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%FA","ú"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%FB","û"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%FC","ü"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%FD","ý"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%FE","þ"); 
INSERT INTO urlcodemap (encoded,decoded) VALUES ("%FF","ÿ"); 

DELIMITER $$ 

DROP FUNCTION IF EXISTS `URLDECODER`$$ 

CREATE FUNCTION `URLDECODER`(str VARCHAR(4096) CHARSET utf8) RETURNS VARCHAR(4096) DETERMINISTIC 
BEGIN 
       DECLARE X INT;    
       DECLARE chr VARCHAR(256); 
       DECLARE chrto VARCHAR(256); 
       DECLARE result VARCHAR(4096); 
       SET X = 1; 
       WHILE X <= (SELECT MAX(id) FROM urlcodemap) DO 
        SET chr = (SELECT `encoded` FROM urlcodemap WHERE id = X); 
        SET chrto = (SELECT `decoded` FROM urlcodemap WHERE id = X);     
          SET str = REPLACE(str,chr,chrto); 
          SET X = X + 1;       
       END WHILE; 
       RETURN str; 
     END$$ 

DELIMITER ;  

用法舉例

SELECT urldecoder('http://testing.com/questions/7031469/is%20this%20working'); 
+0

#1071 - 指定的密鑰太長;最大密鑰長度是767字節 –

+1

@羅曼洛塞夫:謝謝,修正。 – Mistdemon

+0

我不認爲你有足夠的信用這個答案。很有幫助。 –

1

我需要一個解決方案中進行選擇時數據記錄可能包含編碼的URL特殊字符$ & + , / : ; = ? @(空白不包括DED)和做這樣的(示例代碼):

SELECT * FROM `table` WHERE `field` REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(`field`,'%24','$'),'%26','&'),'%2B','+'),'%2C',','),'%2F','/'),'%3A',':'),'%3B',';'),'%3D','='),'%3F','?'),'%40','@') LIKE '/example-request-uri?' 
0

我的答案是非常相似的Mistdemon,但有以下改動:

(1)該線路應該是頂部插入:

INSERT INTO urlcodemap(encoded,decoding)VALUES(「%25」,「%」);

否則的空間出來作爲%2520代替%20由於空間%之前....

(2)如後URL編碼,刪除爲AZ,AZ所有行,和O- 9。郵政服務器絕對可以讀取標準字母和數字......整個事情看起來並不像gobbledy-gook。

+0

這也是非常明顯的,但DECODER函數可以很容易地被重寫爲ENCODER函數,通過交換chr和chrto在行SET str = REPLACE(str,chr,chrto); – PhoenixTech

0

我的解決辦法是聲明存儲函數解碼URL編碼的字符串:

DELIMITER $$ 

DROP FUNCTION IF EXISTS URL_DECODE $$ 

CREATE FUNCTION URL_DECODE (str text) 
RETURNS text 
DETERMINISTIC 
BEGIN 
    DECLARE result text; 
    DECLARE ind INT DEFAULT 0; 

    SET result = REPLACE(str, '+', ' '); 

    WHILE ind <= 255 DO 
     SET result = REPLACE(result, CONCAT('%', LPAD(LOWER(HEX(ind)), 2, 0)), CHAR(ind)); 
     SET result = REPLACE(result, CONCAT('%', LPAD(HEX(ind), 2, 0)), CHAR(ind)); 
     SET ind = ind + 1; 
    END WHILE; 

    RETURN result; 
END$$ 

DELIMITER ; 

而不是用它未來的方式:

SELECT URL_DECODE('johnsmith%40hotmail.com')