Unserialize through query at database level itself

后端 未结 8 530
清酒与你
清酒与你 2020-12-02 12:53

I have a column value stored in the database as:

a:2:{i:0;s:2:\"US\";i:1;s:2:\"19\";}

I want to unserialize it during the mysql

8条回答
  •  渐次进展
    2020-12-02 13:39

    How about this? This is a MySQL user-defined function with embedded php:

    CREATE FUNCTION unserialize_php RETURNS STRING SONAME 'unserialize_php.so';
    

    Usage example:

    SELECT unserialize_php('O:8:"stdClass":2:{s:1:"a";s:4:"aaaa";s:1:"b";s:4:"bbbb";}', "$obj->a") 
    AS 'unserialized';
    
    +--------------+
    | unserialized |
    +--------------+
    | aaaa         |
    +--------------+
    1 row in set (0.00 sec)
    
    drop function unserialize_php;
    

    Source: https://github.com/junamai2000/mysql_unserialize_php

    You can create a MySQL user-defined function and call zend_eval_string inside of the function so that you can bring back PHP variables to a MySQL result. I implemented a sample program. You can try it.

提交回复
热议问题