1

The following query works when the limit clause isn't in place. When I try to use limit no rows are returned. How can I impose a limit on the final result set of this query?

 $testsQuery = "
        select
            testingsession.closed,
            testingsession.id as sessionid,
            testingsession.submittedTests submitted,
            schoolaccount.NameOfSchool as schoolname,
            schoolaccount.ContactEmail as email,
            testtitle,
            date_format(testingsession.createddate, '%%m/%%d/%%Y') as createddate,
            date_format(testingsession.expires, '%%m/%%d/%%Y') as expires,
            credits,
            count(testtaker.id) as completed,
            credits - count(testtaker.id) as available,
        case
            when reporting.sessionid is null then 0
            else 1
        end as reportexists
        from
            schoolaccount
        right join testingsession
        on
            schoolaccount.id = testingsession.schoolid
        inner join thetest
        on
            testingsession.testid = thetest.id
        left outer join testtaker
        on
            testtaker.sessionid = testingsession.id
        left outer join reporting
        on
            testingsession.id = reporting.sessionid
        where %s    
        group by
            testingsession.id
        order by
            createddate DESC
        limit :l1, :l2

    ";

    $where = "1 = 1";
    if(isset($_POST['page'])){
        $pageBegin = 20 * $_POST['page']; 
        $pageEnd = 20 * ($_POST['page'] + 1);
    }else{
        $pageBegin = 0; 
        $pageEnd = 20;
    }
    if(isset($_POST['go'])){
        if(isset($_POST['zip']) && $_POST['zip'] != "zip code"){
            $where = "schoolaccount.zip = :zip or schoolaccount.mailingzip = :zip ";

            $testsStmt = $dbh->prepare(sprintf($testsQuery, $where));

            $testsStmt->bindParam(':zip', $_POST['zip']);
        }else if(isset($_POST['city']) && $_POST['city'] != "city"){
            $where = "schoolaccount.city = :city or schoolaccount.mailingcity = :city ";

            $testsStmt = $dbh->prepare(sprintf($testsQuery, $where));

            $testsStmt->bindParam(':city', $_POST['city']);
        }else if(isset($_POST['state'])){
            $where = "schoolaccount.state = :state or schoolaccount.mailingstate = :state ";

            $testsStmt = $dbh->prepare(sprintf($testsQuery, $where));

            $testsStmt->bindParam(':state', $_POST['state']);
        }
    }else{

        $testsStmt = $dbh->prepare(sprintf($testsQuery, $where));
    }

    $testsStmt->bindParam(':l1', $pageBegin, PDO::PARAM_INT);

    $testsStmt->bindParam(':l2', $pageEnd, PDO::PARAM_INT);

    $testsStmt->execute();

    $testResults = array();
    while($i = $testsStmt->fetch(PDO::FETCH_BOTH)){
        $testResults[] = $i;
    }
Craig Lafferty
  • 771
  • 3
  • 10
  • 31

1 Answers1

3

As you can see in this question, try using :

$testsStmt->bindValue(':l1', (int) trim($pageBegin), PDO::PARAM_INT);
$testsStmt->bindParam(':l2', (int) trim($pageEnd), PDO::PARAM_INT);
Community
  • 1
  • 1
BMN
  • 8,253
  • 14
  • 48
  • 80