php - 需要 MySQL 查询数组和输出到 echo 的帮助

标签 php mysql arrays echo

我需要一些 PHP 编码帮助来构建 echo 以显示数据库查询的结果...(列出登录用户的 friend ?)我已经构建了大部分内容,但我不知道如何回显结果数据并将其链接到他/她的个人资料?任何帮助将不胜感激...也许是如何写出它的代码片段?

这是我的代码:

$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if ($numrows > 0){
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
    array_push($my_friends, $row["user2"]);
    array_push($my_friends, $row["user1"]);
}
//remove your id from array
$my_friends = array_diff($my_friends, array($u));
//reset the key values
$my_friends = array_values($my_friends);
mysqli_free_result($query);
} else {
    echo "You have no friends.";
}

// Build My friends From Results
     ****this is where I need help with*****

if (array_key_exists('0', $my_friends)){
    $sql = "SELECT user1, user2 
    FROM friends 
    WHERE (user1='$u' OR user2='$u')
    AND accepted='1'";      
    $result = mysqli_query($db_conx, $sql);
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

$my_friends = "<a href='user.php?u=".$result."'>". $row['avatar']. '&nbsp'. $row['firstname']. '&nbsp' .$row['lastname'] ."</a>"; }
}
?>

Here is my html code:
<?php
echo "<li>$my_friends</li>"; 
  ?>

最佳答案

您正在 html 链接中使用 $result 变量。将其替换为 $row['id'] 或用作 GET 参数的行属性以查看用户的详细页面

关于php - 需要 MySQL 查询数组和输出到 echo 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42818071/

相关文章:

mysql - 将 NOW() 设置为日期时间数据类型的默认值?

mysql - RENAME DATABASE 在 MySQL 中不再可用吗?

c# - 如何让我的快速排序算法同时按升序和降序对数组进行排序?

php - 使用 join with condition 组合一列

PHP 应用程序重定向周期

mysql - 如何使用wireshark清晰抓取mysql查询sql

java - 如何在android中将位图转换为字节数组,将字节数组转换为位图?

javascript - 对象数组 - 与其他列数据相比增加一列

php - SQL 查询给出了错误的顺序

php - 我应该使用 CodeIgniter 的单元测试类还是 PHPUnit/TOAST?