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

时间:2017-01-26 22:26:24

标签: php mysql database wordpress

这是挑战,我相信这不是第一次有人遇到这个问题,但是在堆栈溢出或任何页面中没有找到与此特定流程相关的可行解决方案其他发展论坛。

以下是我要完成的任务。

我已经建立了一个WordPress网站,该网站将成为一个会员区,只有注册用户才能登录该网站查看其内容。这个功能将通过WordPress完成,但我在WordPress中有一个标题为"会员状态"我已经创建了一个自定义页面模板,以便提取用户特定于该页面上的表单的信息。用户特定信息正从辅助外部数据库提取到WP数据库。

以下是自定义页面模板的代码,以便更好地了解我要执行的操作。基本上所有表单字段都将从该指定用户的特定行填充。但是查询似乎每次都失败了,我想知道是否应该将此表添加到我的WP数据库中,如果这样可以解决问题,或者我的代码中是否存在错误,我只是没有看到。



<?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>
&#13;
&#13;
&#13;

我们将非常感谢所有的见解。在此先感谢社区对新成员的帮助。 :)

更新#2已解决的问题

我很高兴地宣布我发现了这个问题。对于任何想要完成此任务的人来说,这是我使用的工作代码。

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

我希望这对你们所有人都有用:)

1 个答案:

答案 0 :(得分:0)

我很高兴地宣布我发现了这个问题。对于任何想要完成此任务的人来说,这是我使用的工作代码。

<?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 )   {?>

然后调用各个字段/列数据,将此代码添加到您希望位于的位置

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

我希望这对你们所有人都有用:)