Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6044315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:59:51+00:00 2026-05-23T06:59:51+00:00

I have 2 tables usrfrnd_table user_id friend_id __________________ 1 2 2 5 3 1

  • 0

I have 2 tables

usrfrnd_table

user_id friend_id
__________________
1         2

2         5

3          1

2          1

and usr_table

id fullname
________________

1  John Doe

2 George Cann
...

How to echo all friends of for example id 2 from table usr_table with loop?
Tried this piece of code, but it doesn’t work:

$stmt = "SELECT fullname FROM usr_table INNER JOIN usrfrnd_table ON usr_table.id=usrfrnd_table.friend_id WHERE usrfrnd_table.user_id='$id'";
    $result=$db->query($stmt);
    foreach($result as $rec){
        echo "<li><a href=user.php?id=".$rec['id']."'>". $rec['fullname'] . "</a></li>";
    }  

tried this one also doesn’t work:

$stmt = "select fullname from usr_table inner join usrfrnd_table ON (usr_table.id = usrfrnd_table.friend_id) WHERE usrfrnd_table.user_id ='$id'";
$result=$db->query($stmt);
foreach($result as $rec){
echo "<li><a href=user.php?id=".$rec['id']."'>". $rec['fullname'] . "</a></li>";

where am I wrong?

<?php
error_reporting(E_ALL);
require "db.php";

if(isset($_GET['id']))
{
    $id=$_GET['id'];
    $result=$db->query("SELECT * FROM usr_table WHERE id='$id' LIMIT 1") or die(printf("Səhv1: %s\n", $db->error));
    $data=$result->fetch_object();
    $fullname=$data->fullname;
    $dob = $data->dob;
    $phone= $data->phone;
    $adress= $data -> adress;
    $school = $data->school;
    $info = $data->info;}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=$fullname?> | BAZA</title>
<link type="text/css" href="styles/user.css" rel="stylesheet" />

</head>

<body>
<body>
    <div id="address"><?=$adress?></div>
    <h1><?=$fullname?></h1>
    <div id="contact"><?=phone?><a href="" target="_blank" muse_scanned="true">emailaddress@uwaterloo.ca</a></div>

    <div class="section">
        <h2>Summary of Qualifications</h2>
            <ul>
                <li>Bullet one Bullet one</li>
                <li>Bullet two Bullet two</li>
                <li>Bullet three Bullet three</li>
                <li>Bullet four Bullet four</li>
                <li>Bullet five Bullet Five</li>
            </ul>
    </div>

    <div class="section">
        <h2>Work Experience</h2>
        <div class="content">
            <span class="date">Start » End Year</span>
            <h3>Job Title One</h3>
            <address>Company Name, Address, City Province</address>
            <ul>
                <li>Bullet one Bullet one</li>
                <li>Bullet two Bullet two</li>
                <li>Bullet three Bullet three</li>
                <li>Bullet four Bullet four</li>
            </ul>

            <span class="date">Start » End Year</span>
            <h3>Job Title Two</h3>
            <address>Company Name, Address, City Province</address>
            <ul>
                <li>Bullet one Bullet one</li>
                <li>Bullet two Bullet two</li>
                <li>Bullet three Bullet three</li>
                <li>Bullet four Bullet four</li>
            </ul>

            <span class="date">Start » End Year</span>
            <h3>Job Title Three</h3>
            <address>Company Name, Address, City Province</address>
        </div>
    </div>

    <div class="section">
        <h2>Yaxın adamlar</h2>
        <div class="content">
            <span class="date">Dostlar</span>
            <h3>Volunteer Job Title</h3>
            <address>Company Name, Address, City Province</address>
            <ul>
            <?php
$stmt = "select U.* from usr_table U
join usrfrnd_table F on U.id=F.friend_id
where F.user_id='$id'
union distinct select U.* from usr_table U
join usrfrnd_table F on U.id=F.user_id
where F.friend_id='$id'";
    $result=$db->query($stmt);
    foreach($result as $rec){
        echo "<li><a href=user.php?id=".$rec['id']."'>". $rec['fullname'] . "</a></li>";
    }  

                ?>
            </ul>
        </div>
    </div>

    <div class="section">
        <h2>Təhsil</h2>
            <ul>
                <li>Candidate for Bachelor of Major, University, Province,  StartYear - Present</li>
                <li>OSSD, Highschool, Town, Province, StartYear-EndYear</li>
            </ul>
    </div>

    <div class="section">
        <h2>Academic Awards</h2>
            <ul>
                <li>Year, Year, Name of award/Scholarship</li>
            </ul>
    </div>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:59:52+00:00Added an answer on May 23, 2026 at 6:59 am

    I believe that the trouble is in your PHP, not your SQL. Try this:

    $result = $db->query($stmt);
    while ($rec = $result->fetch_array()) {
        echo "<li><a href=user.php?id=".$rec['id']."'>". $rec['fullname'] . "</a></li>";
    }
    

    You appear to be using the mysqli extension. In that case, $db->query() returns a result object, not an array. You have to repeatedly call fetch_array() on the result object to get each row in the result set.

    Also, I recommend that you use this as your query (but that isn’t the immediate issue):

    select U.* from usr_table U
    join usrfrnd_table F on U.id=F.friend_id
    where F.user_id=2
    union distinct select U.* from usr_table U
    join usrfrnd_table F on U.id=F.user_id
    where F.friend_id=2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables usrfrnd_table user_id friend_id __________________ 1 1 2 5 3 1
i have tables driver, cars and trailers they are all in relationship. and main
Suppose i have tables Products -------product_id , name , price , size shopping_cart------cart_id,item_id,user_id,quantity order----order_id
hey all I have tables with millions of rows in them and some of
I have tables as below. Table Messages message_id parent_id forum_id user_id Table Users user_id
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int
I have tables linked by FK, I query on the first table using entity
I have tables named news and tags_news. Given a news id I have to
I have tables Product ( ID, CategoryID, ... ) Category ( ID, Name, ..
I used to have tables before to display the content and people here advised

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.