#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    277
    Rep Power
    0

    Php SELECT IF COUNT




    above is my table in db

    PHP Code:
    <?php
    /*foreach ($_POST as $a=>$b){
            echo $a.":".$b."<br/>";
        }*/
        
    include("config.php");
        
        
    $from     $_POST["from"];
        
    $to     $_POST["to"] ;
        
    $sel     $_POST["select"];
        
    $dur    $_POST["dur"];
        
        
    $sql "
                SELECT billsec, clid , src , lastapp , duration,disposition,SUM(billsec),COUNT(billsec)
                FROM cdr
                WHERE   calldate > '
    $from'
                    AND calldate < '
    $to'
                    AND duration > '
    $dur'
                    AND src = '
    $sel'
                    AND lastapp     != 'busy'
                    AND disposition !='FAILED'
                    AND disposition !='NO ANSWER'
                    AND disposition !='BUSY'
                    
            "
    ;
            
    $result mysql_query($sql);

    if (
    mysql_num_rows($result) == 0) {
            echo 
    "No rows found, nothing to print so am exiting";
        exit;
    }

    ?>
    <table  class="tablo1" border="1">
      <tr>
            <td><div>No :             </div></td>
            <td><div>src :         </div></td>
            <td><div>lastapp :         </div></td>
            <td><div>Type :         </div></td>
            <td><div><?php echo $dur ?>sn U.T.A</div></td>
            <td><div><?php echo $dur ?>sn U.T.S</div></td>

      </tr>

    <?php
    $i
    =0;
    while (
    $row mysql_fetch_array($result)) {   
    $i++;
        echo
    '
            <tr>
                <td><div>'
    .$i                        .' </div></td>
                <td><div>'
    .$row["clid"]                .'</div></td>
                <td><div>'
    .$row["src"]                .'</div></td>
                <td><div>'
    .$row["lastapp"]            .'</div></td>
                <td><div>'
    .$row["COUNT(billsec)"]    .'</div></td>
                <td><div>'
    .$row["SUM(billsec)"]        .'</div></td>
        
            </tr>
            '
    ;
    }

    ?>
    </table>


    i wanna list some values' COUNTand SUM in a table at the same time when lastapp changed bu i cant for example

    when lasapp is dial and src = 110 and all billsec SUM and COUNT
    lastapp is hungup and src = 110 and all billsec SUM and COUNT
    lastapp is wait and src = 110 and all billsec SUM and COUNT
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    first of all, you need to fix several security holes:

    Donít insert raw values into query strings.
    Donít output raw values or insert them into the HTML page.

    After 7 years, I think it might be a good idea to start writing secure and correct code.

    Your query description I don't really understand. How exactly is your output supposed to look like?

    The current query is neither valid nor sensible. MySQL will swallow it, anyway, but on other database systems, this wouldn't even run. You cannot have aggregate functions (SUM, COUNT) and normal columns intermixed (unless they appear in the GROUP BY clause).

    Actually, this is a pure MySQL question, so wait for a moderator to move it.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo