How can I call a row of user specific data from a custom table added to the WP Database

All we need is an easy explanation of the problem, so here it is.

I have setup my WordPress site, customized the Theme as well as a couple plugins, I have also added a custom Table to the WordPress database that will contain user specific information.

I am wanting to call everything in a single row within the table for the specific user.

I have set all my echo statements for the specific column elements of the row however am having difficulty with the initial php to pull the information. No matter what I have tried I keep getting query failed.

Currently I have made it this far however am still having a few issues getting the information to correctly load. any insight would be appreciated. ๐Ÿ™‚

$servername = "localhost";
$username = "DBusername";
$password = "DBpassword";
$database = "DBname";

// Create connection
$conn = new mysqli($servername, $username, $password);

$db_selected = mysqli_select_db($conn, $database);

            $userid = $current_user->user_login;
    $sql = "SELECT * FROM 'member-status' WHERE 'Card' = '$userid'";
            $result = mysqli_query($conn, $sql);
            if ($result) {
                list($userid) = mysqli_fetch_array(mysqli_query($result));
            } else {
                echo "query failed";

    if (mysqli_connect_errno($conn))
      echo "Failed to connect to MySQL: " . mysqli_connect_error();



I have began tweaking the code after a bit of further research and decided to clean up the code a bit, I am now not getting the connection errors I was before however I am still not loading the information. any insight would be grateful, here is the revised code.

<?php global $current_user;
  wp_get_current_user();    ?>

$mydb = new wpdb('user','pwrd','db','localhost');
$userid = $current_user->user_login;
$query = "SELECT * FROM member_status WHERE CARD = $userid";

$result = $mydb->get_results($query);

  <li><?php echo $result["Last"]; ?></li> //need help with this line

Thanks in advance for your insight. ๐Ÿ™‚


I am happy to announce that I figured out the issue. For anyone else that is wanting to accomplish this task here is the working code that I used.

<?php global $current_user;
  wp_get_current_user();    ?>

// $mydb = new wpdb('username','password','database','localhost');
$userid = $current_user->user_login;
$result = $wpdb->get_results( "SELECT * FROM member_status WHERE CARD = $userid");
// $query = "SELECT * FROM member_status WHERE CARD = $userid";
// $result = $mydb->get_results($query);

<?php foreach ( $result as $query )   {?>

Then to call the individual fields/column data add this code where you want it located

<?php echo $query->ColumnName; ?> //Change ColumnName to the column you want to call from.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Aftere long hours of research through the WordPress Codex and a variety of Forum Posts and tutorials. Here is the solution for my current issue.

First I began by calling the current logged in user.

<?php global $current_user;
wp_get_current_user();    ?>

From there I connected to the database. Seeing as I added the table within the WordPress database I used the global statement and selected the database table and information I wanted to as well as prepared it for query.

<?php global $wpdb;
$userid = $current_user->user_login;
$result = $wpdb->get_results( "SELECT * FROM custom_table WHERE CARD = $userid");
// $query = "SELECT * FROM custom_table WHERE CARD = $userid";
// $result = $wpdb->get_results($query);

<?php foreach ( $result as $query )   {?>

Then to call the individual fields/column data for the specific user I simply echoed the various queries of the custom_table’s columns which I wanted to populate on my web page.

<?php echo $query->ColumnName; ?>

I hope some of the community find this helpful. And my edits to this Answer are more detailed for those who come across this post in the future.

Note: Use and implement method 1 because this method fully tested our system.
Thank you ๐Ÿ™‚

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply