[英]How to access multiple mysql rows with same name different table
I think im close to the solution, but not quite getting it. 我认为我接近解决方案,但并不完全得到它。
What im trying to do is get a product.naam and a categorie.naam from 2 different tables. 我想要做的是从2个不同的表中获取product.naam和categorie.naam。 This code below gets me the naam from both and i can acces them both, but now i want everything from product, not just the name and then i don't know how to access it from the $row...
下面这段代码让我从两者中获取naam,我可以同时访问它们,但现在我想要产品的所有内容,而不仅仅是名称,然后我不知道如何从$行访问它...
Every little bit of help is appreciated 每一点点的帮助都表示赞赏
$kerst_sql = " SELECT product.naam, product.prijs AS p_naam, categorie.naam AS c_naam
FROM categorie_product,
product, categorie
WHERE product.product_id = categorie_product.product_product_id
AND categorie_product.categorie_categorie_id = '1'
GROUP BY product.naam";
$result = mysqli_query($con, $kerst_sql);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo $row['p_naam'];
echo $row['c_naam'];
}
If you don't want to explicitly list them all out, you can use SELECT product.*, categorie.naam AS c_naam FROM ...
and then access it like this: 如果您不想明确地列出它们,可以使用
SELECT product.*, categorie.naam AS c_naam FROM ...
然后像这样访问它:
echo $row['naam']; // product.naam
echo $row['prijs']; // product.prijs
echo $row['c_naam']; // categorie.naam
But I think it's usually a good idea to select the specific columns you need (and alias them appropriately) instead of using SELECT *
. 但我认为通常选择所需的特定列(并适当地对其进行别名)而不是使用
SELECT *
通常是个好主意。 IMO, you should have a real reason to want to use the asterisk, and not just that you don't feel like typing out all the column names. IMO,你应该有一个真正的理由想要使用星号,而不仅仅是你不想输入所有的列名。
An example of a situation where I would use SELECT *
is when I know that I need all the columns that currently exist in the table AND any columns that might be added in the future. 的情况下,我会用一个例子
SELECT *
是当我知道我需要的是目前存在于表中的所有列, 并且可能会在将来增加任何列。 For instance, if I were writing an application whose purpose is to display the entire contents of a table. 例如,如果我正在编写一个应用程序,其目的是显示表的全部内容。
And when looping through the columns, you want to take care to keep in mind that at any time, columns can be added or removed. 在循环遍历列时,您需要注意记住,可以随时添加或删除列。 So if you're showing them in an HTML table, for instance, make sure you don't hard-code your column headers, because if a column is added or removed from the table, you'll have an extra or missing column.
因此,例如,如果您在HTML表格中显示它们,请确保不对列标题进行硬编码,因为如果在表格中添加或删除列,则会有一个额外或缺少的列。
If this isn't something that you're trying to do, it might be better to just explicitly list out every column that you want to select. 如果这不是您想要做的事情,那么最好只明确列出您要选择的每一列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.