简体   繁体   English

将Where子句添加到JDatabase

[英]Adding Where Clause To JDatabase

I am receiving 2 params from a querystring in my page url header. 我从页面URL标头中的querystring接收2个参数。 I want to use those parrams as date criteria for a query, but anytime I try to add to my where clause the page returns a 500 error. 我想将这些parrams用作查询的日期标准,但是无论何时我尝试将其添加到我的where子句中,页面都会返回500错误。

This is my syntax, what should I change so that I can use these two variables in my where clause? 这是我的语法,应该更改什么才能在where子句中使用这两个变量?

    <?php

    header('Access-Control-Allow-Origin: *');

    $begin = $_GET['begin'];
    $end = $_GET['end']; 

    $option = array(); //prevent problems

    $option['driver']   = 'mssql';            
    $option['host']     = 'XXX.XXX.XX.XX';    
    $option['user']     = 'user';       
    $option['password'] = 'pass';   
    $option['database'] = 'database';      
    $option['prefix']   = '';             

    $db = JDatabase::getInstance( $option );
    $result = $db->getQuery(true);
    $result->select($db->quoteName(array('Teacher', 'student', 'countenrolled', 'countattending')));
    $result->from($db->quoteName('[SchoolStuff')); 
    $result->where($db->quoteName('registerdate') . ' >= '. $begin. AND ' <= ' .$end.);
    $db->setQuery($result); 
    $results = $db->loadObjectList();
?>

<table border="1">
    <thead>
        <tr>
            <th>Teacher</th>
            <th>Student</th>
            <th>Count Enrolled</th>
            <th>Count Attending</th>
        </tr>
    </thead>
        <tbody>
        <?php
            foreach( $options as $option ) { 
              print "<tr>";
              print "<td>".$option->Teacher."</td>";
              print "<td>".$option->Student."</td>";
              print "<td>".$option->CountEnrolled."</td>";
              print "<td>".$option->CountAttending."</td>";
              print "</tr>";
            } 
            ?>
    </tbody>
  </table

In your WHERE clause, your AND should be quoted. 在您的WHERE子句中,您的AND应该加引号。 Also, in a where clause the field you are comparing need to be compared both times. 另外,在where子句中,您要比较的字段必须同时进行两次比较。

WHERE a >= b AND a <= c a> = b和a <= c

$result->where($db->quoteName('registerdate') . " >= '". $begin."' AND ".$db->quoteName('registerdate')." <= '" .$end."'");

顺便说一句,您可以将数组传递到“ where”

$query->where(array(first thing, second thing, &tc), operator[defaults to "AND"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM