Mysql 自訂函數

來源:互聯網
上載者:User
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;


回複討論(解決方案)

select strip_tag('

aaa

')


update tbl_name set html=strip_tag(html)

select strip_tag('

aaa

')


update tbl_name set html=strip_tag(html)



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;



不會錯的,我剛剛測試了你的指令

你最好寫到程式裡

$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());
因為你使用了 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)

select strip_tag('

aaa

')


update tbl_name set html=strip_tag(html)




自訂函數可以存活多久?

永久,直到你刪去他

永久,直到你刪去他



我加了一句 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


    $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;

按理說後面要加一個分號的
DROP FUNCTION IF EXISTS `strip_tags`;

但是mysql_query 只能執行一條sql語句,所以它把整個語句當成一條sql語句了,當成一條sql顯然是不對的。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.