Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向表列添加标题,并从SQL输出总计_Php_Sql_Mysql - Fatal编程技术网

Php 向表列添加标题,并从SQL输出总计

Php 向表列添加标题,并从SQL输出总计,php,sql,mysql,Php,Sql,Mysql,我有这个密码 $sqlstr = mysql_query( "SELECT * FROM sales where passport = '{$therecord['passport']}'"); if (mysql_numrows($sqlstr) != 0) { echo "<b>Sales for {$therecord['firstname']} {$therecord['lastname']}</b>"; while ($row = mys

我有这个密码

$sqlstr = mysql_query(
    "SELECT * FROM sales where passport = '{$therecord['passport']}'");
if (mysql_numrows($sqlstr) != 0) {
    echo "<b>Sales for {$therecord['firstname']} {$therecord['lastname']}</b>";
    while ($row = mysql_fetch_array($sqlstr)) {
        echo "<table><tr>";
        echo "<td>{$row['product']}</td>";
        echo "<td>{$row['quantity']}</td>";
        echo "<td>{$row['cost']}</td>";
        echo "</tr>";
        echo "</table>";
    }
}

$sqltotal = mysql_query(
    "SELECT SUM(cost) FROM sales where passport = '{$therecord['passport']}'");
echo "<b>Total Owing: {$sqltotal}</b>";
我想给每个表列添加一个标题,也许它们之间有一些间距

我该怎么做呢


此外,我试图使用SQL SUM函数并输出一个总数,但目前它输出的是类似id24的内容…

在启动while循环之前只需回显它即可:


至于求和问题,这是因为您正在回显资源ID。您还需要在该资源上调用mysql_fetch_数组或类似函数,就像在另一个资源上一样。

您可以在while循环之前回显表标题

<thead>
    <tr>
        <th>Name:</th>
        <th>Quantity:</th>
        <th>Cost:</th>
    </tr>
</thead>
<?php
$sqlstr = mysql_query("SELECT * FROM sales where passport = '{$therecord['passport']}'");
if (mysql_numrows($sqlstr) != 0) {
    ....
}
....
?>
这将使用mysql\u结果对求和问题进行排序。目前您只输出资源

$sqltotal = mysql_result(mysql_query("SELECT SUM(cost) FROM sales where passport = '{$therecord['passport']}'"),0);
echo "<b>Total Owing: {$sqltotal}</b>";
只需运行数字或行,因为查询与上面相同

试试这个求和:

$sqltotal = mysql_query("SELECT SUM(cost) AS totalcost FROM sales WHERE passport = '" . mysql_real_esacpe_string($therecord['passport] . "';
$row = mysql_fetch_array($sqltotal);
echo "<b>Total Owing: {$row['totalcost']}</b>";
关于金额: 运行查询后,您需要获得结果

$sqltotal = mysql_query("SELECT SUM(cost) as total FROM sales where passport = '{$therecord['passport']}'");
$row = mysql_fetch_array($sqltotal);
            echo "<b>Total Owing: {$row['total']}</b>";

对于总计,不运行第二个查询,只需将成本转储到一个临时变量,并在通过查询数组循环时递增

$sqlstr = mysql_query(
    "SELECT * FROM sales where passport = '{$therecord['passport']}'");
if (mysql_numrows($sqlstr) != 0) {
    echo "<b>Sales for {$therecord['firstname']} {$therecord['lastname']}</b>";
    while ($row = mysql_fetch_array($sqlstr)) {
        echo "<table><tr>";
        echo "<td>{$row['product']}</td>";
        echo "<td>{$row['quantity']}</td>";
        echo "<td>{$row['cost']}</td>";
        echo "</tr>";
        echo "</table>";
        $_t += $row['cost'];
    }
}
echo "<b>Total Owing: {$_t}</b>";
缺点: 只有在循环完成后才能获得总值


标题。。只需在循环之前打印即可。

我发布了我的回复,但我误读了问题:/这个答案更适用!mysql扩展处于过时状态。改为使用mysqli扩展。@artifact:…最好使用too。默认情况下,mysql\u fetch\u数组允许数字和关联访问。返回类型默认为MYSQL\u两者。MYSQL\u num\u rows不是答案,这将只返回行数,而不是成本的总和。
$sqltotal = mysql_query("SELECT SUM(cost) as total FROM sales where passport = '{$therecord['passport']}'");
$row = mysql_fetch_array($sqltotal);
            echo "<b>Total Owing: {$row['total']}</b>";
$sqlstr = mysql_query( 
    "SELECT * FROM sales where passport = '{$therecord['passport']}'"); 
if (mysql_numrows($sqlstr) != 0) { 
    echo "<b>Sales for {$therecord['firstname']} {$therecord['lastname']}</b>"; 
    echo "<table>";
    echo "<tr>"; 
    echo "<th>Product</th>"; 
    echo "<th>Quantity</th>"; 
    echo "<th>Cost</th>"; 
    echo "</tr>"; 
    while ($row = mysql_fetch_array($sqlstr)) { 
        echo "<tr>"; 
        echo "<td>{$row['product']}</td>"; 
        echo "<td>{$row['quantity']}</td>"; 
        echo "<td>{$row['cost']}</td>"; 
        echo "</tr>"; 
    } 

    $sqltotal = mysql_query( 
        "SELECT SUM(cost) AS Total FROM sales where passport = '{$therecord['passport']}'"); 
    $totalRow = mysql_fetch_array($sqltotal)) { 
    echo "<tr>"; 
    echo "<td colspan=\"2\"></td>"; 
    echo "<td><b>Total Owing: {$totalRow['Total']}</b></td>"; 
    echo "</tr>"; 

    echo "</table>"; 
} 
$sqlstr = mysql_query(
    "SELECT * FROM sales where passport = '{$therecord['passport']}'");
if (mysql_numrows($sqlstr) != 0) {
    echo "<b>Sales for {$therecord['firstname']} {$therecord['lastname']}</b>";
    while ($row = mysql_fetch_array($sqlstr)) {
        echo "<table><tr>";
        echo "<td>{$row['product']}</td>";
        echo "<td>{$row['quantity']}</td>";
        echo "<td>{$row['cost']}</td>";
        echo "</tr>";
        echo "</table>";
        $_t += $row['cost'];
    }
}
echo "<b>Total Owing: {$_t}</b>";