[英]mySQL SELECT two or more rows in the same query
I am using the below code to query an pull Category and Stock of the first 30 Product ID that are store into the $IDs array in PHP. 我正在使用以下代码查询存储在PHP的$ IDs数组中的前30个产品ID的提取类别和库存。
//Category
for ($n = 0; $n < 30; $n++) {
$ID = $IDs[$n];
$result = mysql_query("SELECT Category FROM Products where Code= '$ID'", $link);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
$productCategory[$n] = $row["Category"];
}
}
//Stock
for ($n = 0; $n < 30; $n++) {
$ID = $IDs[$n];
$result = mysql_query("SELECT Stock FROM Products where Code= '$ID'", $link);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
$productStock[$n] = $row["Stock"];
}
}
Is there a way to query and pull both Category and Stock with the same SELECT so i can minimise the queries to mySQL DB to only 30 not 60? 有没有一种方法可以查询和提取具有相同SELECT的Category和Stock,因此我可以将对MySQL数据库的查询最小化到仅30个而不是60个?
SELECT Stock, Category FROM Products WHERE Code= '$ID'
Please note that the mysql_*
methods have been deprecated in PHP due to their vulnerability to SQL injection. 请注意,由于易受SQL注入的攻击,
mysql_*
方法已在PHP中被弃用。 You should use either PDO or mysqli functions. 您应该使用PDO或mysqli函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.