请问一下我的 SQL 为什么报错了呢?是哪里的问题呢? [ 2.0 版本 ]
$result = (new Query())->select([
'ad_id',
'ad_img',
'ad_url',
'ad_name',
'start_time',
'end_time'
])
->from('warehouse_ad')
->where($condition)
->andWhere('<','start_time',date('Y-m-d H:i:s',time()))
->andWhere('>','end_time',date('Y-m-d H:i:s',time()))
->orderBy('ad_sort')
->all();
PHP Warning – yii\base\ErrorException
Invalid argument supplied for foreach()
1. in D:\root\PHPTutorial\WWW\kjy\warehouse\vendor\yiisoft\yii2\db\Query.php at line 1006
9979989991000100110021003100410051006100710081009101010111012101310141015 * @return $this the query object itself
* @see params()
*/
public function addParams($params)
{
if (!empty($params)) {
if (empty($this->params)) {
$this->params = $params;
} else {
foreach ($params as $name => $value) {
if (is_int($name)) {
$this->params[] = $value;
} else {
$this->params[$name] = $value;
}
}
}
}
return $this;
2. in D:\root\PHPTutorial\WWW\kjy\warehouse\vendor\yiisoft\yii2\db\Query.php at line 1006 – yii\base\ErrorHandler::handleError(2, 'Invalid argument supplied for fo...', 'D:\root\PHPTutorial\WWW\kjy\ware...', 1006, ...)
3. in D:\root\PHPTutorial\WWW\kjy\warehouse\vendor\yiisoft\yii2\db\Query.php at line 599 – yii\db\Query::addParams('end_time')
4. in D:\root\PHPTutorial\WWW\kjy\warehouse\common\DataCache.php at line 766 – yii\db\Query::andWhere('>', 'end_time', '2019-03-19 18:22:52')
最佳答案
-
LaravelCode 发布于 2019-03-19 22:28 举报
不应该是这样么?
->andWhere(['<','start_time',date('Y-m-d H:i:s',time())])
共 1 条回复
其他 2 个回答
-
楼上正解,
andWhere($condition, $params = [])
,三个参数值是作为$condition的数组,不存在三个参数的写法,用方法前要看下方法的使用呢。`/*** Adds an additional WHERE condition to the existing one. * The new condition and the existing one will be joined using the `AND` operator. * @param string|array|ExpressionInterface $condition the new WHERE condition. Please refer to [[where()]] * on how to specify this parameter. * @param array $params the parameters (name => value) to be bound to the query. * @return $this the query object itself * @see where() * @see orWhere() */`
PHP学院的中学生
注册时间:2018-10-23
最后登录:2024-09-23
在线时长:168小时13分
最后登录:2024-09-23
在线时长:168小时13分
- 粉丝29
- 金钱4725
- 威望30
- 积分6705