CSkovholm Posted April 26, 2010 Share Posted April 26, 2010 Hey guys, i've got a question. I have a SQl string that goes: "SELECT * FROM products WHERE id IN(20,3,1)" the 3 ids that are shown in the query, represents the product ids for a shopping system. I now have a sql query that retrieves the information about the products. I want to create an array containing the requested values, from the sql query, and then store the array into a variable. Something like this: <?php require "db.php"; //Db connection $sql = "SELECT * FROM products WHERE id IN (20,3,1)"; //Sql $q = mysql_query($sql); //Query $rows = mysql_fetch_assoc($q); //Returned rows $total_price = implode(+, $rows['price']); echo $total_price ?> I know that's not it.. But how is this done? Thanks in advantage! Link to comment https://forums.phpfreaks.com/topic/199764-retrieving-sql-query-values-and-store-them-into-array/ Share on other sites More sharing options...
TeddyKiller Posted April 26, 2010 Share Posted April 26, 2010 How are you wanting it to be displayed? If you're wanting to display the 3 results of Price from the 3 id's given.. try this out. <?php require "db.php"; $sql = "SELECT * FROM products WHERE id IN (20,3,1)"; $q = mysql_query($sql); while($rows = mysql_fetch_assoc($q)) { $price[] = $rows['price']; } $total_price = implode('+', $price); echo $total_price; ?> I'm not sure if that does what you are wanting it to do though. It might bring out a string rather than an answer. Link to comment https://forums.phpfreaks.com/topic/199764-retrieving-sql-query-values-and-store-them-into-array/#findComment-1048491 Share on other sites More sharing options...
CSkovholm Posted April 26, 2010 Author Share Posted April 26, 2010 Thanks! That stored the returned variables into the array.. But how do i plus the variables from the array with eachother, in an implode(); ? Link to comment https://forums.phpfreaks.com/topic/199764-retrieving-sql-query-values-and-store-them-into-array/#findComment-1048506 Share on other sites More sharing options...
de.monkeyz Posted April 26, 2010 Share Posted April 26, 2010 Inside the loop, just add to a $total counter: <?php require "db.php"; $sql = "SELECT * FROM products WHERE id IN (20,3,1)"; $q = mysql_query($sql); $total = 0; //Total of the addition while($rows = mysql_fetch_assoc($q)) { $price[] = $rows['price']; $total += $rows['price']; //Add to the total } $total_price = implode('+', $price); echo $total_price . ' = ' . $total; ?> Alternatively you could just use array_sum: <?php require "db.php"; $sql = "SELECT * FROM products WHERE id IN (20,3,1)"; $q = mysql_query($sql); while($rows = mysql_fetch_assoc($q)) { $price[] = $rows['price']; } $total_price = implode('+', $price); echo $total_price, ' = ', array_sum($price); ?> Link to comment https://forums.phpfreaks.com/topic/199764-retrieving-sql-query-values-and-store-them-into-array/#findComment-1048519 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.