[英]How to echo more than one row with same id
I have a database that allows a user id to appear as many times as the user wants eg id = 1902 the items belonging to this id is fish, bread, milk.我有一个数据库,它允许用户 id 出现用户想要的次数,例如 id = 1902 属于这个 id 的项目是鱼、面包、牛奶。 Same user has with same id on another row id = 1902 the items belonging to this id row is pepper, mat, mouse.
同一用户在另一行 id = 1902 上具有相同的 id,属于该 id 行的项目是胡椒、垫子、鼠标。
I want to try and output the user id and all its items but don't know the code to use on this.我想尝试 output 用户 ID 及其所有项目,但不知道要使用的代码。
<?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.我使用了这段代码,但它正在获取数据库的所有行,包括其他用户的 id,但我希望它只获取在表上出现多次的特定 id 的所有行。
You can get all items and user id from one request SQL您可以从一个请求中获取所有项目和用户 ID SQL
SELECT * FROM investment where userid = (SELECT id FROM users WHERE username = ?)
OR或者
SELECT
*
FROM
investment INV
INNER JOIN
users U ON U.id = INV.userid
WHERE U.username = ?
To select custom rows you must replace ( * ) by the desired value from table对于 select 自定义行,您必须将( * )替换为表中的所需值
I did not understand your problem very well:/我不太了解您的问题:/
You can do it in one query using an inner join, something like this:您可以使用内部联接在一个查询中执行此操作,如下所示:
SELECT
*
FROM
investment
INNER JOIN
users ON users.id = investment.userid
WHERE users.username = ?
That will fetch all records from investment
that has the username you provide.这将从具有您提供的用户名的
investment
中获取所有记录。
You can read more about inner joins here: https://www.mysqltutorial.org/mysql-inner-join.aspx/您可以在此处阅读有关内部联接的更多信息: https://www.mysqltutorial.org/mysql-inner-join.aspx/
As you can see, both me and the other answer use ?
如您所见,我和其他答案都使用
?
instead of the variable with the username.而不是带有用户名的变量。 That's because we use parameterized prepared statements instead of injecting data directly into the queries.
那是因为我们使用参数化的准备好的语句,而不是直接将数据注入到查询中。 I would recommend that you read through that as well, since it's the recommended way of using dynamic data in your queries (and is a necessity when working with external data)
我建议您也通读一遍,因为这是在查询中使用动态数据的推荐方式(并且在处理外部数据时是必要的)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.