2

I've got a few custom user data fields that require user data from a custom database table.

I've tried:

global $wpdb;

$table_name = $wpdb->prefix . "wplusersprofiles";

$user = $wpdb->get_results( "SELECT * FROM $table_name" );

and calling the data like:

<tr>
    <th><label for="gender"><?php _e("Gender"); ?></label></th>
    <td>
        <input type="text" name="gender" id="gender" value="<?php echo $user->gender ?>" class="regular-text" /><br />
    </td>
</tr>

But with no success.

Example table in the DB: enter image description here

1 Answer 1

15

The first section of your code is correct

  global $wpdb;

  $table_name = $wpdb->prefix . "wplusersprofiles";

  $user = $wpdb->get_results( "SELECT * FROM $table_name" );

The problem is in the way you tried to fetch the individual row data. The get_results function in your case returns an object array. So the correct way to fetch individual data should be like...

<?php foreach ($user as $row){ ?>
<tr>
    <th><label for="gender"><?php _e("Gender"); ?></label></th>
    <td>
        <input type="text" name="gender" id="gender" value="<?php echo $row->gender ?>" class="regular-text" /><br />
    </td>
</tr>
<?php } ?>
1
  • Good Stuff. Thanks for the information. It works now. Commented Jun 2, 2017 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.