最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - how to serialize() mysql update data

programmeradmin1浏览0评论

Hi I want to make the serialized reading value 1 or 0 in mysql, how can I do

example code

 $query =  $wpdb->get_results('select * from wp_options where option_id = 96');


    $dizi =   unserialize($query[0]->option_value);
     
      // echo $dizi['subscriber']['capabilities']['read'];

       //print_r($dizi);

   if(array_key_exists('read', $dizi['subscriber']['capabilities'])){


       echo $dizi['subscriber']['capabilities']['read'] = 0;
       

   }

   exit;

Hi I want to make the serialized reading value 1 or 0 in mysql, how can I do

example code

 $query =  $wpdb->get_results('select * from wp_options where option_id = 96');


    $dizi =   unserialize($query[0]->option_value);
     
      // echo $dizi['subscriber']['capabilities']['read'];

       //print_r($dizi);

   if(array_key_exists('read', $dizi['subscriber']['capabilities'])){


       echo $dizi['subscriber']['capabilities']['read'] = 0;
       

   }

   exit;
Share Improve this question asked Nov 12, 2020 at 15:56 Mehmet CemilMehmet Cemil 501 silver badge5 bronze badges 3
  • Do you need to do this through MySQL or can you just use add_cap? I feel like that would be easier than trying to deal with $wpdb. – Howdy_McGee Commented Nov 12, 2020 at 16:19
  • You can't do this with a MySQL query, you must fetch it and deserialize using PHP, make the change, then serialize and save the new serialized value. What exactly are you trying to do that requires this? If you're trying to add or remove a capability to a role there are functions to do that, you don't need to do raw SQL queries, in fact it would be bad practice to do it that way – Tom J Nowell Commented Nov 12, 2020 at 16:46
  • What are you trying to do that requires this? – Tom J Nowell Commented Nov 13, 2020 at 13:21
Add a comment  | 

1 Answer 1

Reset to default 1

You should not use raw SQL to modify roles and capabilities, it is very bad practice, and unnecessary.

Instead, use the roles API, e.g.

$role = get_role( 'subscriber' );
$role->add_cap( 'read' ); 

Or to add it to a specific user:

$user = new WP_User( $user_id );
$user->add_cap( 'read' );

Likewise you can remove a capability from a role:

$role = get_role( 'subscriber' );
$role->remove_cap( 'read' ); 
发布评论

评论列表(0)

  1. 暂无评论