In PHP MySQL 根据搜索结果求和总行数
In PHP MySQL Sum total nos of rows based on search results
在PHPMySQL中,我做了手机商城系统,用户买卖手机,买卖金额以表格的形式存入数据库
我正在检索视图中 table 的所有数据并计算买卖价格的总数。
我也在根据不同的参数进行搜索,例如移动 IMEI、账单号、型号等
但搜索后它会正确显示结果,但我想根据他们的搜索显示手机总价。
if(isset($_POST['submit'])){
$a = $_POST['userInput']; //user enter text for search
$sql = "select * from mobile where mob_bill='$a' or mob_imei='$a' or mob_model='$a'";
$result = mysqli_query($conn,$sql);
$output="";
while($row=mysqli_fetch_array($result)){
$bill = $row['mob_bill'];
$imei = $row['mob_imei'];
$model = $row['mob_model'];
$output.="<tr>
<td>$bill</td>
<td>$imei</td>
<td>$model</td>
</tr>";
}
}
else{
$sql = "select *, Sum(buy_payment) AS buyTotal, Sum(sell_payment) AS sellTotal from mobile";
$result = mysqli_query($conn,$sql);
$output1="";
while($row=mysqli_fetch_array($result)){
$bill = $row['mob_bill'];
$imei = $row['mob_imei'];
$model = $row['mob_model'];
$buy_payment=$row['buyTotal'];
$sell_payment=$row['sellTotal'];
$output1.="<tr>
<td>$bill</td>
<td>$imei</td>
<td>$model</td>
<td>$buy_payment</td>
<td>$sell_payment</td>
</tr>";
}
}
<?php echo $outout; ?>
<?php echo $outout1; ?>
在 SO 上搜索后,我找到了以下答案:-
if(isset($_POST['submit'])){
$a = $_POST['userInput']; //user enter text for search
$query2 = "select sum(buy_price) as buyPrice, sum(sell_price) as sellPrice from mobile where mob_bill='$a' or mob_imei='$a' or mob_model='$a' or mob_status='$a'";
$result2 = mysqli_query($conn,$query2);
$output2="";
while($row2=mysqli_fetch_array($result2)){
$buyPrice = $row2['buyPrice'];
$sellPrice = $row2['sellPrice'];
}
$sql = "select * from mobile where mob_bill='$a' or mob_imei='$a' or mob_model='$a' or mob_status='$a'";
$result = mysqli_query($conn,$sql);
$output="";
while($row=mysqli_fetch_array($result)){
$bill = $row['mob_bill'];
$imei = $row['mob_imei'];
$model = $row['mob_model'];
$output.="<tr>
<td>$bill</td>
<td>$imei</td>
<td>$model</td>
</tr>";
}
}
else{
}
<?php echo $outout; ?>
<?php echo $buyPrice; ?>
<?php echo $sellPrice; ?>
在PHPMySQL中,我做了手机商城系统,用户买卖手机,买卖金额以表格的形式存入数据库
我正在检索视图中 table 的所有数据并计算买卖价格的总数。
我也在根据不同的参数进行搜索,例如移动 IMEI、账单号、型号等
但搜索后它会正确显示结果,但我想根据他们的搜索显示手机总价。
if(isset($_POST['submit'])){
$a = $_POST['userInput']; //user enter text for search
$sql = "select * from mobile where mob_bill='$a' or mob_imei='$a' or mob_model='$a'";
$result = mysqli_query($conn,$sql);
$output="";
while($row=mysqli_fetch_array($result)){
$bill = $row['mob_bill'];
$imei = $row['mob_imei'];
$model = $row['mob_model'];
$output.="<tr>
<td>$bill</td>
<td>$imei</td>
<td>$model</td>
</tr>";
}
}
else{
$sql = "select *, Sum(buy_payment) AS buyTotal, Sum(sell_payment) AS sellTotal from mobile";
$result = mysqli_query($conn,$sql);
$output1="";
while($row=mysqli_fetch_array($result)){
$bill = $row['mob_bill'];
$imei = $row['mob_imei'];
$model = $row['mob_model'];
$buy_payment=$row['buyTotal'];
$sell_payment=$row['sellTotal'];
$output1.="<tr>
<td>$bill</td>
<td>$imei</td>
<td>$model</td>
<td>$buy_payment</td>
<td>$sell_payment</td>
</tr>";
}
}
<?php echo $outout; ?>
<?php echo $outout1; ?>
在 SO 上搜索后,我找到了以下答案:-
if(isset($_POST['submit'])){
$a = $_POST['userInput']; //user enter text for search
$query2 = "select sum(buy_price) as buyPrice, sum(sell_price) as sellPrice from mobile where mob_bill='$a' or mob_imei='$a' or mob_model='$a' or mob_status='$a'";
$result2 = mysqli_query($conn,$query2);
$output2="";
while($row2=mysqli_fetch_array($result2)){
$buyPrice = $row2['buyPrice'];
$sellPrice = $row2['sellPrice'];
}
$sql = "select * from mobile where mob_bill='$a' or mob_imei='$a' or mob_model='$a' or mob_status='$a'";
$result = mysqli_query($conn,$sql);
$output="";
while($row=mysqli_fetch_array($result)){
$bill = $row['mob_bill'];
$imei = $row['mob_imei'];
$model = $row['mob_model'];
$output.="<tr>
<td>$bill</td>
<td>$imei</td>
<td>$model</td>
</tr>";
}
}
else{
}
<?php echo $outout; ?>
<?php echo $buyPrice; ?>
<?php echo $sellPrice; ?>