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

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 } ?>

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *