开发者

Joining mysql tables

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>


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;


Join the table usrfrnd_table using user_id, and not friend_id:

$stmt = "SELECT fullname FROM usr_table INNER JOIN usrfrnd_table ON usr_table.id=usrfrnd_table.user_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>";
    }  


Get rid of the single apostrophes around $id, it's an int, so they aren't necessary. Also, you need to call fetch_assoc on $result and check to make sure it isn't NULL:

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

  $result->free();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜