Jump to content

Need help to display sql query result


dardime

Recommended Posts

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

 

2rgkuud.jpg

<?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>
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.