gpt4 book ai didi

php - 回显两个用户

转载 作者:行者123 更新时间:2023-11-29 07:35:56 25 4
gpt4 key购买 nike

本文底部的代码当前回显:

Name: Spongebob Squarepants

Description: I live in a pineapple under the sea.

Role: editor

但是“wp_usermeta”中有两个用户。这只是一个回响。结果需要如下所示:

Name: wp_dev_05

Description: My name is Chris Topher! I like long walks on the beach and bags of pork rinds.

Role: administrator

Name: Spongebob Squarepants

Description: I live in a pineapple under the sea.

Role: editor

$query = mysqli_query($conn, "SELECT `meta_value` as `value` FROM `wp_usermeta` WHERE `meta_key` = 'first_name'") or die(mysqli_error($conn));
while ($row = mysqli_fetch_assoc($query)) {
$first_name = $row["value"];
}

$query = mysqli_query($conn, "SELECT `meta_value` as 'value' FROM `wp_usermeta` WHERE `meta_key` = 'last_name'") or die(mysqli_error($conn));
while ($row = mysqli_fetch_assoc($query)) {
$last_name = $row["value"];
}

$query = mysqli_query($conn, "SELECT `meta_value` as 'value' FROM `wp_usermeta` WHERE `meta_key` = 'description'") or die(mysqli_error($conn));
while ($row = mysqli_fetch_assoc($query)) {
$description = $row["value"];
}

$query = mysqli_query($conn, "SELECT `meta_value` as 'value' FROM `wp_usermeta` WHERE `meta_key` = 'wp_capabilities'") or die(mysqli_error($conn));
while ($row = mysqli_fetch_assoc($query)) {
$role_array = $row["value"];
}

//Creates wp_capability meta_value as a string.
$role= unserialize($role_array);
while ($role_index = current($role)) {
if ($role_index == 1) {
$user_role = key($role);
}

next($role);
}
echo '<b>Name: </b>';
echo $first_name . ' ';
echo $last_name . '</br>';
echo '<b>Description: </b>';
echo $description . '</br>';
echo '<b>Role: </b>';
echo $user_role . '</br>';

我该如何写才能得到我想要的结果?感谢您的帮助!

最佳答案

为什么不在一个查询中完成它呢?只需替换必要的表名和列名,以及要在查询中绑定(bind)的变量/值/参数即可:

$query = mysqli_query($conn, "SELECT first_name, last_name, description, role FROM `wp_usermeta`
WHERE `first_name` = '$first_name'
OR `last_name` = '$last_name'
OR `description` = '$description'
OR `role` = '$role'") or die(mysqli_error($conn));

while ($row = mysqli_fetch_array($query)) {
echo "<b>Name: </b>".$row["first_name"]." ".$row["last_name"]."<br>";
echo "<b>Description: </b>".$row["description"]."<br>";
echo "<b>Role: </b>".$row["role"]."<br>";
}

关于php - 回显两个用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836476/

25 4 0