douxing1969 2017-02-28 19:38
浏览 41
已采纳

将Where子句添加到JDatabase

I am receiving 2 params from a querystring in my page url header. 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.

This is my syntax, what should I change so that I can use these two variables in my where clause?

    <?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
  • 写回答

2条回答 默认 最新

  • duan19780629 2017-02-28 19:42
    关注

    In your WHERE clause, your AND should be quoted. Also, in a where clause the field you are comparing need to be compared both times.

    WHERE a >= b AND a <= c

    $result->where($db->quoteName('registerdate') . " >= '". $begin."' AND ".$db->quoteName('registerdate')." <= '" .$end."'");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?