如何在php中汇总表数据值

时间:2015-05-06 09:38:09

标签: php mysql

如何使用php汇总表数据值但不选择mysql表

mysql_query("SELECT  * FROM pay_detec  WHERE (`nic_num` LIKE '%".$query."%') OR (`cust_name` LIKE '%".$query."%')"); 
if(mysql_num_rows($raw_results) > 0)
{
while($results = mysql_fetch_array($raw_results))
{ 

echo "<tr align='CENTER' bgcolor='FBE946'>

<td height='25px'>".$results['cust_name']."</td> <td>".$results['nic_num']."</td><td>".$results['acc_num']."</td><td>".$results['due_date']."</td>
<td>".$results['loan']."</td><td>".$results['due_months']."</td> <td>".$results['blanc_due']."</td><td>".$results['month_pay']."</td>
<td >".$results['payment']."</td><td>".$results['INTRST']."<td bgColor=red colSpan=0>".$results['short']."</td><td>".$results['othr_chrg']."</td><td>".$results['DELAY_CHRG']."</td><td>".$results['ADVNC']."</td><td>".$results['ttl']."</td><td>".$results['blanc2']."</td></tr>" ;
}

2 个答案:

答案 0 :(得分:1)

就像Honza所说,当你循环遍历行时,使用变量来总结。 你也可以使用mysqls WITH ROLLUP和你的组(https://dev.mysql.com/doc/refman/5.0/en/group-by-modifiers.html

我还要看看如何保护你的SQL作为上面的查询,如果你正在使用它,将$ query作为用户输入,将让你对SQL注入开放。

示例:

$totalCol = 0;
    mysql_query("SELECT  * FROM pay_detec  WHERE (`nic_num` LIKE '%".$query."%') OR (`cust_name` LIKE '%".$query."%')"); 
    if(mysql_num_rows($raw_results) > 0)
    {
        while($results = mysql_fetch_array($raw_results))
        { 
            $totalCol += $results['a_col'];
            echo "<tr align='CENTER' bgcolor='FBE946'>
            <td height='25px'>".$results['cust_name']."</td> <td>".$results['nic_num']."</td><td>".$results['acc_num']."</td><td>".$results['due_date']."</td>
            <td>".$results['loan']."</td><td>".$results['due_months']."</td> <td>".$results['blanc_due']."</td><td>".$results['month_pay']."</td>
            <td >".$results['payment']."</td><td>".$results['INTRST']."<td bgColor=red colSpan=0>".$results['short']."</td><td>".$results['othr_chrg']."</td><td>".$results['DELAY_CHRG']."</td><td>".$results['ADVNC']."</td><td>".$results['ttl']."</td><td>".$results['blanc2']."</td></tr>";
        }
        /* row for totals */
    }

答案 1 :(得分:0)

this my full code 

<?php

     echo '<body style="background-color:046C33">';


     echo '<body style="background-color:orange">';

    mysql_connect("127.0.0.1", "root", "") or die(mysql_error());
    mysql_select_db("customers") or die(mysql_error());
    ?>
    <html>
    <head>
    <title>Search</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <form method="post" >
    <table border="0" cellpadding="0" cellspacing="0">
    <tr>ENTER NIC NUMBER
    <td><input type="text" name="query" id="text" />&nbsp;</td>
    <td><input type="submit" name="submit" id="search" value="Search" /></td>
    </tr>
    </table>
    </form>

    <?php 

    if(isset($_POST['submit']))
    {
    $query = $_POST['query']; 
    $min_length = 1;
    if(strlen($query) >= $min_length)
    { 
    $query = htmlspecialchars($query); 
    $query = mysql_real_escape_string($query); 
    echo "<table border='0' width='' align='center' cellpadding='1' cellspacing='1'>";
    echo "<tr align='center' bgcolor='62E15E'> 

    <td height='35px' width='150px'>NAME</td> <td>NIC NUMBER</td><td>GVS AC/NUM</td><td>DUE DATE</td><td>AMNT</td><td>DUE</td>
    <td>DUE BALANCE</td><td>PAYMNET MONTH</td><td>AMOUNT</td><td>INTERST</td><td bgColor=red colSpan=0>SHORT</td><td>OTHER CHRG</td>
    <td>DELAY CHRG</td><td>ADVANCE</td><td>TOTAL DEDEC</td><td>BALANCE</td></tr>"; 
    $totalCol = 0;
    $raw_results = 

        mysql_query("SELECT  * FROM pay_detec  WHERE (`nic_num` LIKE '%".$query."%') OR (`cust_name` LIKE '%".$query."%')"); 
        if(mysql_num_rows($raw_results) > 0)
        {
            while($results = mysql_fetch_array($raw_results))
            { 

                echo "<tr align='CENTER' bgcolor='FBE946'>
                <td height='25px'>".$results['cust_name']."</td> <td>".$results['nic_num']."</td><td>".$results['acc_num']."</td><td>".$results['due_date']."</td>
                <td>".$results['loan']."</td><td>".$results['due_months']."</td> <td>".$results['blanc_due']."</td><td>".$results['month_pay']."</td>
                <td >". $results['payment']."</td><td>".$results['INTRST']."<td bgColor=red colSpan=0>".$results['short']."</td><td>".$results['othr_chrg']."</td><td>".$results['DELAY_CHRG']."</td><td>".$results['ADVNC']."</td><td>".$results['ttl']."</td><td>".$results['blanc2']."</td></tr>";
            }



    }
    else{ 
    echo "<tr align='center' bgcolor='#6C0000'>

    <td colspan='2' height='25px'>No results</td><tr>"; 
    echo "</table>"; 
    } 
    }
    else{ 
    echo "Minimum length is ".$min_length;

     }
    }
    echo "<a href='index.html'>HOME PAGE</a>";
    ?>

    </body>
    </html>