这是挑战,我相信这不是第一次有人遇到这个问题,但是还没有在堆栈溢出或任何页面中找到与此特定过程相关的可行解决方案与此相关的其他发展论坛。
这是我要完成的任务。
我建立了一个 WordPress 网站,该网站将成为成员(member)区,只有注册用户才能登录该网站查看其内容。此功能将通过 WordPress 完成,但是我在 WordPress 中有一个名为“成员(member)状态”的页面,我已经为其创建了一个自定义页面模板,以便提取用户特定于该页面上的表单的信息。正在将用户特定信息从辅助外部数据库提取到 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>
所有见解将不胜感激。提前感谢社区对新成员的帮助。 :)
更新 #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.
希望这对大家有用:)
最佳答案
我很高兴地宣布我已经解决了这个问题。对于任何其他想要完成此任务的人来说,这里是我使用的工作代码。
<?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.
希望这对大家有用:)
关于php - 如何将用户特定数据从外部数据库调用到 WordPress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41884097/