如何根据包含相同值的另一行来计算行值?

时间:2015-06-24 08:44:31

标签: php mysql pdo sum

我想根据cellfun(@(x)(x + 0./~isinf(x)), dataC, 'UniformOutput', false) 获取number of gusts 用户可以通过输入date

来搜索number of guests日期

我的表格是:

date

我的行动部分是:

<form action="index.php" method="post" class="booking_reference">
    <input type="text" placeholder="date" required name="search_text">
    <span class="arrow">
        <input type="submit" value="CHECK" name="search" class="search_button">
    </span>
</form>

我的数据库中有以下行:

Database

期待结果是:

  1. 如果我在搜索字段if (isset($_POST['search'])){ $serch_text = $_POST["search_text"]; $con=mysqli_connect("localhost","root","","guest"); // Check connection if (mysqli_connect_errno()) { die ("Failed to connect to MySQL: " . mysqli_connect_error()); } $searching_date = $serch_text; $searchroute = "select * from guest_tbl where date = '$searching_date'"; $result = $con->query($searchroute); $row = $result->fetch_assoc(); if ($row == 0) { echo '<div style="color: red;">No one found for the date: <b>' . $serch_text . '</b>. Please refine your date</div>' ; } else { echo '<h3>Total guest for the day : '. $serch_text .'</h3>'; $total_guests = mysqli_query($con, 'SELECT date SUM(nmbr_of_guests) FROM guest WHERE date = "'.$serch_text.'"'); // selecting and summing the value of all the 'nmbr_of_guests' rows in same date - $serch_text echo $total_guests; mysqli_close($con); } } 中输入 2015-06-30 应该回应 5 - (因为2 + 3)
  2. 如果我在搜索字段$echo $total_guests中输入 2015-06-25 应该回显 6 - (因为$echo $total_guests列只有6个 日期)
  3. 如果我在搜索字段中输入任何其他日期,则应回显否 找到的日期:任何其他日期。请改进 你的约会对象。
  4. 第三个没问题我找不到日期时会以红线显示该文字。

    但是当我进入 2015-06-30 2015-06-25 时,我只是空白。 这意味着nmbr_of_guests不会返回任何/空白。

    如何使$total_guests;显示日期的$total_guests;行的值(在这种情况下为5或6)?

    注意:它是一个旧网站。完成此操作后,我会将所有 MySQLi 替换为 PDO 。它是一个大文件,它需要时间来替换找到MySQLi的所有行。

2 个答案:

答案 0 :(得分:2)

日期和总和中缺少

逗号...查询应该是 -

 $total_guests = mysqli_query($con, 'SELECT date, SUM(nmbr_of_guests) FROM guest WHERE date = "'.$serch_text.'"'); // selecting and summing the value of all the 'nmbr_of_guests' rows in same date - $serch_text 

<强>更新

您的代码 -

echo '<h3>Total guest for the day : '. $serch_text .'</h3>';        
        $total_guests = mysqli_query($con, 'SELECT date SUM(nmbr_of_guests) FROM guest WHERE date = "'.$serch_text.'"'); // selecting and summing the value of all the 'nmbr_of_guests' rows in same date - $serch_text     
        echo $total_guests;
        mysqli_close($con);

尝试按以下方式检查 -

    $total_guests = mysqli_query($con, 'SELECT SUM(nmbr_of_guests) FROM guest WHERE date = "'.$serch_text.'"'); // selecting and summing the value of all the 'nmbr_of_guests' rows in same date - $serch_text     

echo '<h3>Total guest for the date of '. $serch_text .' are '.$total_guests.'</h3>';        
    mysqli_close($con);

答案 1 :(得分:-1)

你能不能这样试试

 $total_guests = mysqli_query($con, 'SELECT date,nmbr_of_guests FROM guest_tbl WHERE date = "'.$serch_text.'"'); 

指定一个变量“0”并计算值

$total = 0;
foreach ($total_guests as $guests){
$total = $total + $guests->nmbr_of_guests;
}
echo $total;
相关问题