我目前正在运行多个mysql查询但是想要运行一个并循环遍历它。我怎样才能改善这个?
$query = mysql_query("SELECT productid, code, size, quantity, sizechart, rprice, price, weight FROM inventory WHERE code = '$productcode1'");
while ($rows1 = mysql_fetch_array($query))
{
$productids1 = $rows1['productid'];
$codes1 = $rows1['code'];
$sizes1 = ucwords($rows1['size']);
$quantitys1 = $rows1['quantity'];
$sizechart = $rows1['sizechart'];
$rprice = $rows1['rprice'];
$sprice = $rows1['price'];
$dweight = $rows1['weight'];
}
然后每个查询都进行相同但$ productcode2和$ productcode3等。
答案 0 :(得分:6)
... WHERE代码IN('$ productcode1','$ productcode2',...)
答案 1 :(得分:1)
更改“where code ='$ prouctcode1'”on“where code IN(...)”
答案 2 :(得分:1)
一种解决方案是使用PDO's prepared statements。
基于PHP参考的快速,未经测试和脏的示例:
$stmt = $dbh->prepare("SELECT productid, code, size, quantity, sizechart, rprice, price, weight FROM inventory WHERE code = :code");
$stmt->bindParam(':code', $code);
foreach ($productCodes as $code) {
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Do something with the results
}