dardime Posted March 28, 2017 Share Posted March 28, 2017 Hi I need help to display my sql query result on a page. I got working on the sql query but not displaying on the page. Thank you <?php include_once("localhost.php"); $sql="SELECT SUM(`stockinprice` * quantity) AS total FROM stock_stockin,stock_stockin_product,stock_product WHERE stock_date>='1225450800' AND stock_date<='1451559600' AND stock_product.id=stock_stockin_product.product_id AND stock_stockin.id=stock_stockin_product.stockin_id"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { echo $row['total']; ?> <?php }?> Total: <b><?php echo $total; ?></b> Quote Link to comment Share on other sites More sharing options...
benanamen Posted March 28, 2017 Share Posted March 28, 2017 Why did you go back to using the obsolete and dangerous mysql_* code? Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted March 28, 2017 Share Posted March 28, 2017 Hi I need help to display my sql query result on a page. I assume you are referring to the following line: Total: <b><?php echo $total; ?></b> If so, $total hasn't been set. Your loop only displays the $row variable. It doesn't set $total. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 28, 2017 Share Posted March 28, 2017 1, Don't use the mysql_ functions 2. Use proper JOINs. I don't know if you even need those JOINs. What tables do stockinprice, quantity and stock_date come from? 3. Use a valid date type for date fields 4. Create a variable ($total) and add to it on each iteration while looping through the results 5. Use comments in your code I left the code using mysql_ only because you would need to also modify the code in the localhost.php file <?php //Connect to DB include_once("localhost.php"); //Create and execute query $sql = "SELECT SUM(stockinprice * quantity) AS total FROM stock_stockin JOIN stock_stockin_product ON stock_stockin.id = stock_stockin_product.stockin_id JOIN stock_product ON stock_stockin_product.product_id = stock_product.id WHERE stock_date BETWEEN '1225450800' AND '1451559600'"; $result = mysql_query($sql); //Create total variable and increment from results $total = 0; while($row = mysql_fetch_array($result)) { $total += $row['total']; } ?> Total: <b><?php echo $total; ?></b> Quote Link to comment Share on other sites More sharing options...
dardime Posted March 28, 2017 Author Share Posted March 28, 2017 Hi Thank you Psycho but i got 0(zero) result. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted March 28, 2017 Share Posted March 28, 2017 Maybe if you showed your revised code we could help you with it. Quote Link to comment Share on other sites More sharing options...
dardime Posted April 3, 2017 Author Share Posted April 3, 2017 Thanks to all of you. I have sorted this out. Cheers! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.