[英]Is there a way I can select multiple rows in MYSQL based on the IDs saved in another row of another table?
<?php
$sqlstr = mysql_query("SELECT * FROM outfits")or die(mysql_error());
if (mysql_numrows($sqlstr) != 0) {
while ($row = mysql_fetch_array($sqlstr)) {
$sqlstr2 = mysql_query("SELECT * FROM products WHERE pid in ($row['tid'], $row['did'])")or die(mysql_error());
?>
<p><?= $row['pname'] ?></p>
<p><?= $row['pcat'] ?></p>
<p><?= $row['pimg1'] ?></p>
<?php
}
}
?>
You can do it in one query: 您可以在一个查询中执行此操作:
SELECT p.* FROM outfits AS o
INNER JOIN products AS p
ON p.pid IN (o.tid, o.did)
This has the additional benefit that you're not blindly pasting PHP values into your query (which is generally a big red flag, because unless you're really really careful, you're opening up a big box of SQL injection vulnerabilities). 这样做还有一个好处,就是您不会盲目地将PHP值粘贴到查询中(通常这是一个很大的危险信号,因为除非您非常谨慎,否则您会打开一大盒SQL注入漏洞)。
试试这个$sqlstr2 = mysql_query("SELECT * FROM products WHERE pid in (select id from second table where condition )")or die(mysql_error());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.