ds19891231 2017-01-26 22:26
浏览 205
已采纳

如何将外部数据库中的用户特定数据调用到WordPress中

Here is the challenge and I am sure this is not the first time someone has came into this issue however have not found a viable solution pertaining to this specific process within the pages of stackoverflow or any other development forum for that matter.

Here is the task I am trying to complete.

I have built a WordPress website which will be a members area where only registered users may log into the site to view its content. This functionality will be done through WordPress however I have a page within WordPress titled "Member Status" that I have created a custom page template for in order to pull information that will be user specific to a form on that page. The user specific information is being pulled from a secondary external database to the WP database.

Here is the code for the custom page template for a better visual of what I am trying to do. Basically all form fields would be populated from the specific row of that specified user. however the query seems to fail everytime and am wondering if I should be adding this table to my WP db, if that would rectify the issue or is there a fault in my code I am just not seeing.

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

?>
<?php 
    while ($row = mysqli_fetch_assoc("$sql"))
?>
<div class="Disclaimer_webpage">
<?php echo $row["Disclaimer_WebPage"]; ?>
</div>

<div class="OuterBlock">
  
  <div class="oneblock">
    <ul>
      <li>LAST</li>
      <li><?php echo $row["Last"]; ?></li>
    </ul>
    <ul>
      <li>FIRST</li>
      <li><?php echo $row["First"]; ?></li>
    </ul>
    <ul>
      <li>CARD</li>
      <li><?php echo $row["Card"]; ?></li>
    </ul>
    <ul>
      <li>CLASS</li>
      <li><?php echo $row["Class"]; ?></li>
    </ul>
  </div>
  
  
  
  <div class="secblock">
    <ul>
      <li>DATE SIGNED BOOK</li>
      <li>
    <?php    if ($row['StartDate']!=""){         echo date('m/d/y', strtotime($row["StartDate"]));   } ?>
      </li>
    </ul>
    <ul>
      <li>OUT OF WORK DATE</li>
      <li>
    <?php if($row['CURROOWD']!=""){  echo date('m/d/y', strtotime($row["CURROOWD"]));    } ?>
      </li>
    </ul>
    <ul>
      <li>DISPATCH UNIT</li>
      <li><?php echo $row["OriginalUnit"]; ?></li>
    </ul>
    <ul>
      <li>DAYS DISPATCHED (OF 140)</li>
      <li><?php echo $row["DAYS140"]; ?></li>
    </ul>
  </div> 
  
  <div class="over_all_list_position">
    <ul>
      <li><b>OVER ALL LIST POSITION</b><span class="information_Icon"><img src="http://redsealcreative.com/clients/353-members/wp-content/plugins/IBEWJOBLISTS/Info.png" > <p class="imgDescription">Your position on the Out of Work List.</p></span><span class="see_below"> (*see below)</span></li>
      <li><?php echo $row["ListPosition"]; ?></li>
                        
        </ul>
        <ul>
            <li><b>UNIT LIST POSITION</b><span class="information_Icon1"><img src="http://redsealcreative.com/clients/353-members/wp-content/plugins/IBEWJOBLISTS/Info.png" > <p class="imgDescription1">Your list position relative only to members in your Dispatch Unit.</p><span></li>
                             <li><?php echo $row["LISTPOS_UNIT"]; ?></li>
                    </ul>
        <ul>
            <li><b>OPG LIST POSITION</b><span class="information_Icon2"><img src="http://redsealcreative.com/clients/353-members/wp-content/plugins/IBEWJOBLISTS/Info.png" > <p class="imgDescription2">Your list position relative only to those with current OPG Security Clearance.</p><span></li>
                             <li><?php echo $row["LISTPOS_OPG"]; ?></li>
                    </ul>
    </div>
    
    <div class="fothblock">
      <ul>
        <li>PASSES</li>
        <li>NORTH UNIT</li>
        <li>SOUTH UNIT</li>
        <li>EAST UNIT</li>
      </ul>
      <ul>
        <li>ACCUMULATED</li>
        <li><?php echo $row["PassNorthAcc"]; ?></li>
        <li><?php echo $row["PassSouthAcc"]; ?></li>
        <li><?php echo $row["PassEastAcc"]; ?></li>
      </ul>
      <ul>
        <li>MAXIMUM</li>
        <li><?php echo $row["PassNorthMax"]; ?></li>
        <li><?php echo $row["PassSouthMax"]; ?></li>
        <li><?php echo $row["PassEastMax"]; ?></li>
      </ul>
            
    </div>
    
    <div class="sixblock">
      <ul>
        <li>LAST OUT WORK DATES DISPATCHED</li>
        <li>NORTH</li>
        <li>SOUTH</li>
        <li>EAST</li>
        <li>OUT OF TOWN</li>
      </ul>
      <ul>
        <li class="padding-top"></li>
        <li class="padding-top"><?php if($row['LOOWDN']!=""){echo date('m/d/y', strtotime($row["LOOWDN"])); }?></li>
        <li><?php if($row['LOOWDS']!=""){ echo date('m/d/y', strtotime($row["LOOWDS"])); } ?></li>
        <li class="padding-top"><?php if($row['LOOWDE']!=""){echo date('m/d/y', strtotime($row["LOOWDE"]));} ?></li>
        <li><?php if($row['OOTOOWD']!=""){echo date('m/d/y', strtotime($row["OOTOOWD"]));} ?></li>
      </ul>
    </div>
    
    <div class="seven">
      <ul>
        <li>LAST UPDATED</li>
      </ul>
      <ul>
        <li class="padding-top"><?php echo $row["TIME_STAMP"]; ?></li>
      </ul>
    </div>
    
</div>

<div class="Disclaimer_POL"> 
<?php echo $row["Disclaimer_POL"]; ?>
</div>

All insight will be gratefully appreciated. Thanks in advance for the communities assistance to a new member. :)


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.

I hope this comes in handy for you all :)

</div>
  • 写回答

1条回答 默认 最新

  • duangengruan2144 2017-01-27 20:33
    关注

    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 custom_table WHERE Column = $userid");
    // $query = "SELECT * FROM custom_table WHERE Column = $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.
    

    I hope this comes in handy for you all :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办