Site icon Hip-Hop Website Design and Development

Retriving array size from serialized data

I’ve a custom field in WordPress user meta, which store data in serialized array. I want to retrieve the number of arrays. For e.g.: a:2:{blah blah}



Screenshot of my table


I tried the following code but it doesn’t works. It should print 2

$number = $wpdb->get_var("
    SELECT count(*)
    FROM wp_usermeta
    WHERE meta_key='bookmark_posts' AND meta_value LIKE '%".get_current_user_id()."%'
");
if ( $number !== '' ) {
    echo 'Total bookmarks (' . $number . ')';
}

I’ve an error log. (john_newsite is the database name)

WordPress database error Table 'john_newsite.wp_usermeta' doesn't
exist for query SELECT count(*) FROM wp_usermeta WHERE
meta_key='bookmark_posts'

edit:

I just tried the following code. It works but counts the total bookmarks from all users. How can limit the result by current logged in user?

$meta_key='bookmark_posts';
$number = $wpdb->get_var( $wpdb->prepare( 
    "SELECT sum(meta_value) 
    FROM $wpdb->postmeta 
    WHERE meta_key = %s", 
    $meta_key
) );
echo "{$number}";