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

5

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 } ?>
  • Good Stuff. Thanks for the information. It works now. – Callum Jun 2 '17 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.