Home > Backend Development > PHP Tutorial > Mysql 自定义函数

Mysql 自定义函数

WBOY
Release: 2016-06-23 13:04:24
Original
1083 people have browsed it

Mysql 自定义函数怎么用

CREATE FUNCTION `strip_tags`($str text) RETURNS text BEGIN     DECLARE $start, $end INT DEFAULT 1;     LOOP         SET $start = LOCATE("<", $str, $start);         IF (!$start) THEN RETURN $str; END IF;         SET $end = LOCATE(">", $str, $start);         IF (!$end) THEN SET $end = $start; END IF;         SET $str = INSERT($str, $start, $end - $start + 1, "");     END LOOP; END;
Copy after login


回复讨论(解决方案)

select strip_tag('<p>aaa</p>')
Copy after login
Copy after login
Copy after login


update tbl_name set html=strip_tag(html)
Copy after login
Copy after login
Copy after login

select strip_tag('<p>aaa</p>')
Copy after login
Copy after login
Copy after login


update tbl_name set html=strip_tag(html)
Copy after login
Copy after login
Copy after login



mysql里执行下面代码报错

CREATE FUNCTION `strip_tags`($str text) RETURNS text  BEGIN      DECLARE $start, $end INT DEFAULT 1;      LOOP          SET $start = LOCATE("<", $str, $start);          IF (!$start) THEN RETURN $str; END IF;          SET $end = LOCATE(">", $str, $start);          IF (!$end) THEN SET $end = $start; END IF;          SET $str = INSERT($str, $start, $end - $start + 1, "");      END LOOP;  END;
Copy after login



不会错的,我刚刚测试了你的指令

你最好写到程序里

$sql =<<< 'SQL'CREATE FUNCTION `strip_tags`($str text) RETURNS text  BEGIN      DECLARE $start, $end INT DEFAULT 1;      LOOP          SET $start = LOCATE("<", $str, $start);          IF (!$start) THEN RETURN $str; END IF;          SET $end = LOCATE(">", $str, $start);          IF (!$end) THEN SET $end = $start; END IF;          SET $str = INSERT($str, $start, $end - $start + 1, "");      END LOOP;  END;SQL;mysql_query($sql) or die(mysql_error());
Copy after login
因为你使用了 php 的变量名表示,phpmyadmin 可能另做了处理

mysql_connect();mysql_select_db('test');$sql =<<< 'SQL'CREATE FUNCTION `strip_tags`($str text) RETURNS text  BEGIN      DECLARE $start, $end INT DEFAULT 1;      LOOP          SET $start = LOCATE("<", $str, $start);          IF (!$start) THEN RETURN $str; END IF;          SET $end = LOCATE(">", $str, $start);          IF (!$end) THEN SET $end = $start; END IF;          SET $str = INSERT($str, $start, $end - $start + 1, "");      END LOOP;  END;SQL;//mysql_query($sql) or die(mysql_error());$rs = mysql_query("select strip_tags('ab')");print_r(mysql_fetch_row($rs));
Array(    [0] => ab)
Copy after login

select strip_tag('<p>aaa</p>')
Copy after login
Copy after login
Copy after login


update tbl_name set html=strip_tag(html)
Copy after login
Copy after login
Copy after login




自定义函数可以存活多久?

永久,直到你删去他

永久,直到你删去他



我加了一句 DROP FUNCTION IF EXISTS `strip_tags`; 就报错了

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE FUNCTION `strip_tags`($str text) RETURNS text BEGIN DECLARE $start,' at line 2
Copy after login


    $sql =<<< 'SQL'DROP FUNCTION IF EXISTS `strip_tags`CREATE FUNCTION `strip_tags`($str text) RETURNS textBEGIN    DECLARE $start, $end INT DEFAULT 1;    LOOP        SET $start = LOCATE("<", $str, $start);        IF (!$start) THEN RETURN $str; END IF;        SET $end = LOCATE(">", $str, $start);        IF (!$end) THEN SET $end = $start; END IF;        SET $str = INSERT($str, $start, $end - $start + 1, "");    END LOOP;END;SQL;
Copy after login

按理说后面要加一个分号的
DROP FUNCTION IF EXISTS `strip_tags`;

但是mysql_query 只能执行一条sql语句,所以它把整个语句当成一条sql语句了,当成一条sql显然是不对的。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template