tags:

views:

57

answers:

4

im currently running multiple mysql querys but want to just run one and loop through it. how can i improve this

$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']; 
 } 

each query then carrys on the same, but $productcode2 and $productcode3 etc.

+6  A: 

... WHERE code IN ('$productcode1','$productcode2',...)

jamietre
by using where in, how would l then fetch results for each row that matches each productcode? like in the example above. row1, row2 etc?
beam
I am not sure I understand. You already have a loop `while ($rows1 = mysql_fetch_array($query))` which would iterate through each row. The query would return rows that matched any of the productcodes in your list when using where..in
jamietre
+1  A: 

Change "Where code = '$prouctcode1'" on "where code IN (...)"

bswietochowski
+1  A: 

One solution would be using PDO's prepared statements.

Quick, untested and dirty example based on PHP reference:

$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
}
nnevala
Argh, totally misunderstood the question. Well, try this if you actually want to run multiple queries in a more efficient way.
nnevala
A: 

thanks for the help. managed to get want i wanted with an array from the first query.

beam