将 Where 子句添加到 JDatabase
Adding Where Clause To JDatabase
我从我的页面中的查询字符串接收到 2 个参数 url header。我想将这些参数用作查询的日期条件,但任何时候我尝试向我的 where 子句添加页面 returns 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
在你的 WHERE 子句中,你的 AND 应该被引用。此外,在 where 子句中,您要比较的字段需要进行两次比较。
WHERE a >= b AND 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"]);
我从我的页面中的查询字符串接收到 2 个参数 url header。我想将这些参数用作查询的日期条件,但任何时候我尝试向我的 where 子句添加页面 returns 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
在你的 WHERE 子句中,你的 AND 应该被引用。此外,在 where 子句中,您要比较的字段需要进行两次比较。
WHERE a >= b AND 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"]);