php mysql数据库获取总价格

php mysql数据库获取总价格,php,mysql,Php,Mysql,我在mysql数据库中使用php代码将数据显示到html表中,我在listview上使用filter,现在我在过滤后用get price Total堆栈列表。我在php代码方面做得不够好,是不是在php或sql方面出错了 MySQL表: id INT- 名称VARCHAR- 代码VARCHAR- 价格整数 以下是我目前的代码: <?php if(isset($_POST['search'])) { $valueToSearch = $_POST['valueToSearch']; //

我在mysql数据库中使用php代码将数据显示到html表中,我在listview上使用filter,现在我在过滤后用get price Total堆栈列表。我在php代码方面做得不够好,是不是在php或sql方面出错了

MySQL表:

id INT- 名称VARCHAR- 代码VARCHAR- 价格整数

以下是我目前的代码:

<?php

if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `toy` WHERE CONCAT(`id`, `name`, `code`, `price`) 
LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);

}
else {
$query = "SELECT * FROM `toy` LIMIT 5";
$search_result = filterTable($query);
}

// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "user", "pass", 
"database");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}

?>

<!DOCTYPE html>
<html>
<head>
    <title>Wu</title>
    <link href="style.css" type="text/css" rel="stylesheet" />

</head>
<body>

    <div id="toys-grid"> 
        <div class="search-box">
        <br>            
        <form action="index.php" method="post">
        <input type="text" name="valueToSearch" placeholder="Name To Search">
        <br><br>
        <input type="submit" name="search" value="Filter"><br><br>
        </div>

        <table cellpadding="10" cellspacing="1">

    <thead>
            <tr>
                <th>Id</th>
                <th>First Name</th>
                <th>Code</th>
                <th>Price</th>
            </tr>
    </thead>
  <!-- populate table from mysql database -->
            <?php while($row = mysqli_fetch_array($search_result)):?>
            <tbody>
            <tr>
                <td><?php echo $row['id'];?></td>
                <td><?php echo $row['name'];?></td>
                <td><?php echo $row['code'];?></td>
                <td><?php echo $row['price'];?></td>
            </tr>
            </tbody>
            <?php endwhile;?>

            <thead>
            <tr>
            <th> Total </th>
        <th> 
        <?php
        $results = mysql_query('SELECT SUM(price) FROM toy');
        $results->execute();
        for($i=0; $rows = $results->fetch(); $i++){
        echo $rows['SUM(price)'];
        }
        ?>
        </th>
        </tr>
        </thead>
        </table>
        </form>
    </div>
</body>

全部的


非常感谢您的帮助……

无需执行查询以获取总和。你可以用php来做

<table cellpadding="10" cellspacing="1">

    <thead>
            <tr>
                <th>Id</th>
                <th>First Name</th>
                <th>Code</th>
                <th>Price</th>
            </tr>
    </thead>
  <!-- populate table from mysql database -->
    <?php $sum = 0; ?>
    <?php while($row = mysqli_fetch_array($search_result)):?>
    <tbody>
    <tr>
        <td><?php echo $row['id'];?></td>
        <td><?php echo $row['name'];?></td>
        <td><?php echo $row['code'];?></td>
        <td><?php echo $row['price'];?></td>
        <?php
        $sum += $row['price'];
        ?>
    </tr>
    </tbody>
    <?php endwhile;?>

    <thead>
      <tr>
        <th> Total </th>
        <th> 
        <?php
        echo $sum;
        ?>
        </th>
      </tr>
    </thead>
</table>

身份证件
名字
代码
价格
全部的

不明白您想要什么?你想要总计?你在mysql_查询中遗漏了一个'i'('从玩具中选择总和(价格)');您的for循环在第二个参数上的语法错误。更好地使用while语句。例如:while($rows=mysqli_fetch_array($result)){echo$rows['SUM(price)]}完美…非常好用,谢谢,伙计