SELECT maxAmount FROM活动 - SELECT COUNT(活动)FROM注册?

时间:2018-06-09 13:48:27

标签: php pdo

我正在研究从学校毕业的项目,但现在我遇到了一个问题。 我的两个问题是:

$sql = 'SELECT Amount FROM activities ORDER BY Id ASC';

$select = 'SELECT COUNT(Activity) as Activity 
           FROM `registrations` 
           INNER JOIN activities 
                ON registrations.Activity = activities.Name 
           WHERE registrations.Activity='.$row['Name'].' 
                AND registrations.Morning/Afternoon='.$row['Morning/Afternoon'].'';

所以需要发生的是$ sql查询中的金额 - 来自$ select查询的COUNT(活动)。

到目前为止,我得到了这个,但它并不像我希望的那样工作:

<?php

$sql = 'SELECT * FROM activities ORDER BY Id ASC';
foreach ($db->query($sql) as $row) {
    $select = 'SELECT COUNT(Activity) as Activity 
               FROM `registrations` 
               INNER JOIN activities 
                  ON registrations.Activity = activities.Name 
               WHERE registrations.Activity=' . $row['Name'] . ' 
                  AND registrations.Morning/Afternoon=' . $row['Morning/Afternoon'] . '';

    foreach ((array)$select as $select2) {
        $maxAantal = $row['Amount'] - $select2['Activity'];
    }

    echo '<tr>';
    echo '<td>' . $row['Name'] . '</td>';
    echo '<td>' . $row['Location'] . '</td>';
    echo '<td>' . $row['Begintime'] . '</td>';
    echo '<td>' . $row['Endtime'] . '</td>';
    echo '<td>' . $maxAmount . '</td>';
    echo '<td>' . $row['Deadline'] . '</td>';
    echo '<td>' . $row['Morning/Afternoon'] . '</td>';
    echo '<td>';
    echo '<a class="btn btn-secondary" href="readActi.php?id=' . $row['Id'] . '">Read</a>';
    echo '</td>';
    echo '</tr>';
}

$_SESSION['Id'] = $row['Id'];

?>

但它给了我这个警告

Warning: Illegal string offset 'Activity'

我无法弄清楚如何解决这个问题

2 个答案:

答案 0 :(得分:0)

您的第二个查询$select未执行也未获取,只会在变量$select中设置...您只是忘记查询并获取它,这就是为什么$select2['Activity']未设置的原因

检查您的第二个foreach,我认为它应该是foreach ($db->query($select) as $select2)

精确编辑

在您的代码中,您有:

$select = 'SELECT COUNT(Activity) as Activity 
           FROM `registrations` 
           INNER JOIN activities 
              ON registrations.Activity = activities.Name 
           WHERE registrations.Activity=' . $row['Name'] . ' 
              AND registrations.Morning/Afternoon=' . $row['Morning/Afternoon'] . '';

然后

foreach($select as $select2)

此处$select是包含您的查询的字符串。通过对其进行操作,您将只是逐个获取字符串的所有字符,然后索引['activity']将不存在。

答案 1 :(得分:0)

<?php

$sql = 'SELECT * FROM activities ORDER BY Id ASC';
if ($db->query($sql)) {
    foreach ($db->query($sql)->fetchAll(PDO::FETCH_ASSOC) as $row) {
        $select = 'SELECT COUNT(Activity) as Activity 
                   FROM `registrations` 
                   INNER JOIN activities 
                      ON registrations.Activity = activities.Name 
                   WHERE registrations.Activity=' . $row['Name'] . ' 
                      AND registrations.Morning/Afternoon=' . $row['Morning/Afternoon'] . '';
        if ($db->query($select)) {
        foreach ($db->query($select)->fetchAll(PDO::FETCH_ASSOC) as $select2) {
            $maxAantal = $row['Amount'] - $select2['Activity'];
        }

        echo '<tr>';
        echo '<td>' . $row['Name'] . '</td>';
        echo '<td>' . $row['Location'] . '</td>';
        echo '<td>' . $row['Begintime'] . '</td>';
        echo '<td>' . $row['Endtime'] . '</td>';
        echo '<td>' . $maxAmount . '</td>';
        echo '<td>' . $row['Deadline'] . '</td>';
        echo '<td>' . $row['Morning/Afternoon'] . '</td>';
        echo '<td>';
        echo '<a class="btn btn-secondary" href="readActi.php?id=' . $row['Id'] . '">Read</a>';
        echo '</td>';
        echo '</tr>';
        }
    }
}

$_SESSION['Id'] = $row['Id'];

?>

尝试考虑这段代码,在查询SQL语句后必须使用fetchAll才能使用foreach,而不再需要将其显式声明为数组。

由此:

  

foreach($ db-&gt; query($ sql)as $ row){

对此:

  

foreach($ db-&gt; query($ sql) - &gt; fetchAll(PDO :: FETCH_ASSOC)为$ row){

然后这个:

  

foreach((array)$ select as $ select2){

对此:

  

foreach($ db-&gt; query($ select) - &gt; fetchAll(PDO :: FETCH_ASSOC)as $ select2){

编辑:对于布尔问题,只需在每个声明中添加IF声明,请参阅上面的代码