如何回显某些方面的所有行

时间:2018-01-15 06:54:40

标签: php html mysql sql

我有两张桌子。它们通过一个(userinfo)连接到许多(成就)外键关系。我在下面尝试做的是回显所有具有给定$ usrid的行。这可能不止一个。

不幸的是,它只回显了其中一行。如何更改它以回显某个用户ID存在的所有行?

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

3 个答案:

答案 0 :(得分:0)

您可以使用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'];
}

答案 1 :(得分:0)

如果您有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

答案 2 :(得分:0)

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