0

I have two tables. They are connected via a one (userinfo) to many (achievements) foreign key relationship. What I am attempting to do below is echo all the rows which have the given $usrid. This could be more than one.

Unfortunately, It only echos content one of the rows. How can I change it to echo all the rows where a a certain userid is present?

<!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>
dowo3
  • 1
  • 1

3 Answers3

0

You can use while loop to print all row

$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'];
}
omkara
  • 974
  • 5
  • 24
  • 50
0

Where you have the PHP echo statement you can replace it with something like...

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

You should look at JOIN to simplify all of this for you. https://www.w3schools.com/sql/sql_join.asp

This is a pretty good/easy to understand usage of PHP Loops. https://www.tutorialspoint.com/php/php_loop_types.htm

Billy
  • 126
  • 1
  • 10
  • OMKARA has a more in-depth answer. I would still take a look at learning the JOIN clause. – Billy Jan 15 '18 at 07:16
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
Mazhar Hussain
  • 125
  • 1
  • 11