开发者

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 d开发者_运维百科ie(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).


try this $sqlstr2 = mysql_query("SELECT * FROM products WHERE pid in (select id from second table where condition )")or die(mysql_error());

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜