gpt4 book ai didi

php - 连接 mysql 表

转载 作者:行者123 更新时间:2023-11-29 00:55:16 27 4
gpt4 key购买 nike

我有两张 table

usrfrnd_table

user_id friend_id
__________________
1 2

2 5

3 1

2 1

和 usr_table

id fullname
________________

1 John Doe

2 George Cann
...

如何使用循环从表 usr_table 中回显例如 id 2 的所有 friend ?试过这段代码,但它不起作用:

$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>";
}

试过这个也不行:

$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>";

我哪里错了?

<?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>

最佳答案

我认为问题出在您的 PHP,而不是您的 SQL。试试这个:

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

您似乎在使用 mysqli 扩展。在这种情况下,$db->query() 返回结果对象,而不是数组。您必须对结果对象重复调用 fetch_array() 才能获取结果集中的每一行。

此外,我建议您将其用作查询(但这不是紧迫的问题):

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;

关于php - 连接 mysql 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6399129/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com