如何用特定的方面回显所有行

e0bqpujr  于 2021-06-24  发布在  Mysql
关注(0)|答案(3)|浏览(239)

我有两张table。它们通过一对多(userinfo)外键关系连接。下面我要做的是回显具有给定$usrid的所有行。这可能不止一个。
不幸的是,它只回显其中一行的内容。如何将其更改为回显某个用户标识所在的所有行?

<!DOCTYPE HTML>
<head>
<?php  $usrid = $_GET['usrid'];
$connection = @mysqli_connect("localhost","root","","Rain")
OR die('Could not connect' .
      mysqli_connect_error());

$query = "SELECT usrid, username, oldname, languages, joindate, art, hunting, frontwebdev, backwebdev, writing, programming, se, smm, pentesting, timezone, availability, reliability, profilePicture FROM userinfo WHERE usrid='" . $usrid . "';";
$response = @mysqli_query($connection,$query);
$row = @mysqli_fetch_array($response);
$username = $row['username'];

$achvquery = "SELECT achieveid, usrid, achievementname, achievementdescr, timestamp FROM achievements WHERE usrid=" . $usrid . ";";
$achvresponse = @mysqli_query($connection,$achvquery);
$achvrow = @mysqli_fetch_array($achvresponse);
$achvtitle = $achvrow['achievementname'];
$achvdescr = $achvrow['achievementdescr'];
?>
<title>
All Achievements
</title>
</head>
<body>
<span> <?php echo "<span> " . $username . "s OD Achievement History "; ?> </span>

<span id="newAchvLink"> <?php echo "<a id='addNewLink' href='addachievement.php?usrid=" . $usrid . "'> Add new</a>"; ?></span>
<br /> <?php echo "<h2> Achv: </h2>  <h3 class='achvtitle'>" . $achvtitle . "</h3>"; echo $achvdescr;?><br /><br />
</body>
</html>
r7xajy2e

r7xajy2e1#

可以使用while循环打印所有行

$query = "SELECT usrid, username, oldname, languages, joindate, art, hunting, frontwebdev, backwebdev, writing, programming, se, smm, pentesting, timezone, 
 availability, reliability, profilePicture FROM userinfo WHERE usrid='" . $usrid . "';";
$response = @mysqli_query($connection,$query);
while ($row = @mysqli_fetch_array($response))
{
    echo $row['username'];
}
4uqofj5v

4uqofj5v2#

如果有php echo语句,您可以用以下内容替换它。。。

while($row = $result->fetch_assoc($response)) {
 echo "<span> " . $username . "s Op Achievement History ";

你应该看看join来简化这一切。https://www.w3schools.com/sql/sql_join.asp
这是一个非常好/易于理解的php循环用法。https://www.tutorialspoint.com/php/php_loop_types.htm

xam8gpfp

xam8gpfp3#

first table query
while loop
{
$userid=first_table_data['user_id'];
2nd table query where userid=$userid
while loop
{

}

all value save in array
}
print value

相关问题