SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘user_id‘ in ‘where clause‘

2022-07-28,,,,

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'where clause'

  • 原因有很多,分享下遇到的其中一种
    • 报错
    • 报错原因(~写代码一定要细心啊~)
    • 错误代码
    • 正确代码

原因有很多,分享下遇到的其中一种

报错

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_id' in 'where clause'

报错原因(写代码一定要细心啊)

在foreach 中的数组没有重新定义
$where作为where中的查询条件,在foreach中使用过一次之后,又增加了新的元素,当再一次foreach循环时,新增的元素就会在第一次使用时报错!

错误代码

foreach{
	' ' '
	' ' '
	if($end > $start){
		$where['year_month'] = substr($one[1],0,7);
	}else{
		$where['year_month'] = substr($value['date'],0,7);
	}
	$bool = model('Workday')->where($where)->find();//已经用了$where
	if($bool){
		return json('error','请先设置工作日历~');
	}
	$where['user_id'] = $uid;// 问题所在,又给$where增加了新的元素
	$lock = model('AttendanceLock')->where($where)->find();
	if($lock){
		continue;
	}
		' ' ' 
		' ' '
}

正确代码

foreach{
	' ' '
	' ' '
	$where = array(); //这里给$where重新定义一下就行了
	if($end > $start){
		$where['year_month'] = substr($one[1],0,7);
	}else{
		$where['year_month'] = substr($value['date'],0,7);
	}
	$bool = model('Workday')->where($where)->find();//已经用了$where
	if($bool){
		return json('error','请先设置工作日历~');
	}
	$where['user_id'] = $uid;// 问题所在,又给$where增加了新的元素
	$lock = model('AttendanceLock')->where($where)->find();
	if($lock){
		continue;
	}
		' ' ' 
		' ' '
}

本文地址:https://blog.csdn.net/The_My_World/article/details/109637082

《SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘user_id‘ in ‘where clause‘.doc》

下载本文的Word格式文档,以方便收藏与打印。