I have a database that allows a user id to appear as many times as the user wants e.g. id = 1902 the items belonging to this id is fish, bread, milk. Same user has with same id on another row id = 1902 the items belonging to this id row is pepper, mat, mouse.
I want to try and output the user id and all its items but don't know the code to use on this.
<?php
$xyttt = $_SESSION['email'];
$sql = "SELECT id FROM users where username = '$xyttt'";
$result = mysqli_query($db, $sql);
// fetch the resulting rows as an array
$investment = mysqli_fetch_all($result, MYSQLI_ASSOC);
foreach($investment as $invest){
// echo ($invest['id']);
$uidbb = ($invest['id']);
}
// free the $result from memory (good practise)
mysqli_free_result($result);
$uidbb = ($invest['id']);
$sql = "SELECT * FROM investment where userid = '$uidb'";
$result = mysqli_query($db, $sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["amount1"]. " " . $row["status"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
// free the $result from memory (good practise)
mysqli_free_result($result);
?>
I used this code but it was fetching all the rows of the database including the id of other users but I want it to fetch all rows of just a particular id that appears as many times on the table.