0
我已經使用php代碼與mysql數據庫顯示數據到html表,我在listview上使用過濾器,現在我是堆棧與得到的價格過濾後的列表總數。我用php代碼不夠好,我用php或sql犯了錯誤嗎?php mysql數據庫得到的價格總計
MySQL表:
ID INT - 名VARCHAR - 代碼VARCHAR - 價格INT
這裏是我到目前爲止的代碼:
<?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>
我很欣賞你幫助...
沒有明白你想要什麼?你想要總數? –
你錯過了'我'在** mysql_query('選擇SUM(價格)從玩具'); **和你的for循環得到錯誤的語法第二個參數。更好地使用while語句。 E.G:while($ rows = mysqli_fetch_array($ result)){echo $ rows ['SUM(price)']; } –