Need help getting a piece of data from the Wordpress database?

5 replies
I want to extract a piece of data for the logged in user from the Wordpress database, so that I can use it as a variable in another script. The data is the value in a key:value pair in the meta_value field.

Retrieving data from the Wordpress database seems confusing as all get out to me. I saw the $wpdb function, but after that I get lost.

Anyone here what can help with the SQL for that? I use MySQL on a Linux server.

Many thanks in advance,
#data #database #piece #wordpress
  • Profile picture of the author Joshua Kwentoh
    Had the same problem and will be happy for someone to share info how its done
    {{ DiscussionBoard.errors[1111116].message }}
  • Profile picture of the author john_kennedy
    Did you read the information on this page:

    Function Reference/wpdb Class WordPress Codex

    Spells it out for you pretty well.

    {{ DiscussionBoard.errors[1111137].message }}
  • Profile picture of the author jumboa7
    I think you want to query data directly from wordpress database.
    The table containing user information is wp_users. You can read schema from link gave by john_kennedy.

    After login and connect to wp database. I think you can use this SQL command.
    "select * from wp_users"
    {{ DiscussionBoard.errors[1111828].message }}
  • Profile picture of the author n7 Studios
    Assuming you've stored the user meta data in the correct way (i.e. it's in the wp_usermeta table), you can just use:

    PHP Code:
    See Function Reference/get usermeta WordPress Codex for a full function reference.
    {{ DiscussionBoard.errors[1112531].message }}
    • Profile picture of the author Kirk Ward
      Many thanks to all ... but it seems that old retired bean counters have mental blocks from all the numbers they read over the years ... meaning, I still have problems.

      I had previously tried reading the function reference for the wpdb class (which I called the $wpdb function in my original post), but it wasn't basic or simple enough for me. My understanding of code is limited to cut and paste. (That's what happens after thrity years as an accountant, you get stupidiated.)

      Based on Tim's (N7) line of code, I tried
       = get_usermeta(userID, 'metakey'); 
      echo  . "n";
      in the html of a wordpress page and got nothing.

      edit: seems the $mydata and the \ before the n disappeared from the code block ... sorry.

      My user_id value is 1
      My meta_key value is dr_extra_fields
      The meta_value field for that record contains ...
      s:203:"a:6:{s:10:"dr_address";s:11:"PO Box 1787";s:7:"dr_city";s:7:"Ellijay";
      s:14:"(706) 698-5475";s:10:"dr_bp_name";s:20:"Kirk Ward Accounting";}";
      My goal is to pull any one of the values from those key:value pairs and use it on a page for the logged in user.

      Any further ideas?

      Thanks in advance.
      "We are not here to sell a parcel of boilers and vats, but the potentiality of growing rich beyond the dreams of avarice."

      Dr. Samuel Johnson (Presiding at the sale of Thrales brewery, London, 1781)
      {{ DiscussionBoard.errors[1112696].message }}

Trending Topics