1

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.
4
  • You should use the wpdb class for querying custom tables.
    – Milo
    Commented Jan 27, 2017 at 5:13
  • would you be able to provide an example for the first one I added to the code by chance? I am researching the page you noted now but am still seeing double vision at this point lol. Commented Jan 27, 2017 at 5:31
  • 1
    Hi @DaveRomsey Thanks for the tip, I have also now updated the answer for greater detail as well as marked it as answered. I hope this helps the community. I am new and still learning how the websites process works but am loving the contribution and being able to contribute. Commented Jun 7, 2017 at 4:19
  • Good on you, @DigitalDesigner for following through and kicking it up a notch! :-) Commented Jun 7, 2017 at 4:55

1 Answer 1

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.

Your Answer

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

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