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

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. 🙂

<?php
$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();
    }

?>

UPDATE #1

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();    ?>

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

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

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

Thanks in advance for your insight. 🙂


UPDATE #2 ISSUE RESOLVED

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();    ?>

<?php 
// $mydb = new wpdb('username','password','database','localhost');
//$mydb->show_errors();
$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.

Solutions Collecting From Web of "How can I call a row of user specific data from a custom table added to the WP Database"

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.