MYSQL PHP Help එකක් ඕනේ

ArunaTelshan

Well-known member
  • Nov 29, 2013
    3,255
    1,216
    113
    Biyagama
    දන්නවනම් උදවු..............................................................................................................................................



    මන් ගාව ටේබල් එකක් තියෙනවා

    id|date|type|catagory|amount|

    මට ඕනේ මේකෙන් මේ මාසේ income ටිකේ ටෝටල් එක අරගන්න. කොහොමද මේක කරන්නේ..

    Code:
    [SIZE="3"]SELECT 
        EXTRACT(MONTH FROM date) as month, 
        EXTRACT(YEAR FROM date) as year, 
        SUM(amount)
    FROM 
        log
        WHERE type = 'income' 
    GROUP BY 
        month, 
        year[/SIZE]

    මේ විදියට ගත්ත එත් මෙතනින් එහාට කරන්නේ කොහොමද කියල තේරෙන්නෑ :confused::confused::confused:


     
     

    ArunaTelshan

    Well-known member
  • Nov 29, 2013
    3,255
    1,216
    113
    Biyagama
    SELECT
    EXTRACT(MONTH FROM date) as month,
    EXTRACT(YEAR FROM date) as year,
    SUM(amount)
    FROM
    log
    WHERE type = 'income' AND YEAR(date) = 2018 AND MONTH(date) = 4

    meke 4i 2018i wenuwata currnt year ekai month ekai ganne kohomada? :confused:
     

    chaturanga836

    Well-known member
  • Aug 12, 2015
    3,361
    1,324
    113
    Malabe
    ඕක පී.එච්.පී වලින් නම් මෙහෙම පුළුවන්

    $year = date(''y");
    $month = date("m);

    $start_date =$year."-".$month."-01";
    $end_date = $year."-".$month."-31";

    $query="SELECT
    EXTRACT(MONTH FROM date) as month,
    EXTRACT(YEAR FROM date) as year,
    SUM(amount)
    FROM
    log
    WHERE type = 'income' AND
    ( date BETWEEN ='".$start_date."' AND '".$end_date."')";
     
    • Like
    Reactions: ArunaTelshan

    ArunaTelshan

    Well-known member
  • Nov 29, 2013
    3,255
    1,216
    113
    Biyagama
    ඕක පී.එච්.පී වලින් නම් මෙහෙම පුළුවන්

    $year = date(''y");
    $month = date("m);

    $start_date =$year."-".$month."-01";
    $end_date = $year."-".$month."-31";

    $query="SELECT
    EXTRACT(MONTH FROM date) as month,
    EXTRACT(YEAR FROM date) as year,
    SUM(amount)
    FROM
    log
    WHERE type = 'income' AND
    ( date BETWEEN ='".$start_date."' AND '".$end_date."')";

    man code eka me widiyata wenas kala.. sql eken mata ona result eka enawa eth php eken result eka display wenne nane bn.. error ekak enawa..

    $month = date('n');
    $year = date('Y');

    echo $month . "<br>";
    echo $year;

    $connection = mysqli_connect('localhost', 'root', '', 'daily_log');

    $query = "
    SELECT
    SUM(amount)
    FROM
    log
    WHERE type = 'income' AND YEAR(date) = $year AND MONTH(date) = $month
    ";

    $i_total = mysqli_query($connection, $query);

    echo $i_total;

    ub qwa eka try karala balannam.. thanku++
     

    ArunaTelshan

    Well-known member
  • Nov 29, 2013
    3,255
    1,216
    113
    Biyagama
    thanks hamotama wade goda dagaththa while loop ekak dala..

    PHP:
    <?php
    
    $month = date('n');
    $year = date('Y');
    
    $connection = mysqli_connect('localhost', 'root', '', 'daily_log'); 
    
    $iquery = "
    SELECT 
    SUM(amount)
    FROM log
    WHERE type = 'income' AND YEAR(date) = $year AND MONTH(date) = $month
    ";
    
    $iresult = mysqli_query($connection, $iquery);
    while($row = mysqli_fetch_assoc($iresult)){
          $itotal = $row['SUM(amount)'];
    	$itotal; }
    
    $equery = "
    SELECT 
    SUM(amount)
    FROM log
    WHERE type = 'expense' AND YEAR(date) = $year AND MONTH(date) = $month
    ";
    
    $eresult = mysqli_query($connection, $equery);
    while($row = mysqli_fetch_assoc($eresult)){
          $etotal = $row['SUM(amount)'];
    	$etotal; }
    
    
    ?>
     

    wolvoring

    Well-known member
  • Apr 8, 2017
    1,065
    122
    63
    www.rumansala.com & 127.0.0.1
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Wolvoring @ ....</title>
    </head>

    <body>
    <?php

    $host="localhost";
    $username="";
    $password="";
    $db_name="*********";
    $tbl_name="******";


    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");


    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);
    ?>
    <table width="400" border="1" cellspacing="0" cellpadding="3">
    <tr style="background-color:#DDD; color:#09F">
    <td width="10%" align="center">No</td>
    <td width="30%" align="center">Date</td>
    <td width="30%" align="center">Type</td>
    <td width="30%" align="center">Catagory</td>
    <td width="30%" align="center">Amount</td>
    </tr>
    <?php


    while($rows=mysql_fetch_array($result)){
    $amount_total +=$rows['amount'];

    ?>
    <tr>
    <td width="10%"><?php echo $rows['id']; ?></td>
    <td width="30%"><?php echo $rows['date']; ?></td>
    <td width="30%"><?php echo $rows['type']; ?></td>
    <td width="30%"><?php echo $rows['catagory']; ?></td>
    <td width="30%"><?php echo $rows['amount']; ?></td>
    </tr>
    <?php

    }

    ?>
    <tr>
    <td colspan="4">Total Income</td>
    <td><?php echo $amount_total ;?></td>
    </tr>
    </table>
    </body>
    </html>