【问题标题】:Adding Where Clause To JDatabase将 Where 子句添加到 JDatabase
【发布时间】:2017-02-28 19:38:27
【问题描述】:

我从我的页面 url 标题中的查询字符串接收 2 个参数。我想使用这些参数作为查询的日期条件,但是每当我尝试添加到我的 where 子句时,页面都会返回 500 错误。

这是我的语法,我应该更改什么以便可以在 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

【问题讨论】:

    标签: php html sql-server joomla joomla3.0


    【解决方案1】:

    在您的 WHERE 子句中,应引用您的 AND。此外,在 where 子句中,您要比较的字段需要进行两次比较。

    在哪里 a >= b AND a

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

    【讨论】:

    • 开始加载页面 - 但抛出操作数类型冲突的错误:日期与 int 不兼容。我知道 registerdate 是一个日期数据类型 - 所以我假设这两个问题是 $begin 和 $end
    • 打印这两个变量。
    • begin = 2017-01-02 end = 2017-01-25 并且这是基于的观点 - 我正在做的 SchoolStuff registerdate=Cast(registration_date As Date)
    • 您需要引用您的日期,它们被解释为 int,2017 - 01 - 02 = 2014。请参阅我编辑的答案。
    • 这会引发“0 - 已更改数据库上下文”的错误
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多