제가 아래와 같이 sql문을 만들어서 실행하면
어떤때는 db의 데이터를 정상적으로 가져오는데,
어떤때는 db의 데이터를 일부만 가져옵니다.
예를 들어 총 데이터수가 17건이면 정상적으로 가져올때는 17건을 가져옵니다.
어떤때는 2건, 16건 이런식으로 데이터를 가져옵니다.
어떤 부분을 수정해야 할까요?
$params3 = $params1 = $params = $search = [];
$search1 = "";
$sql = "SELECT mgm.member_srl, m.regdate, GROUP_CONCAT(mgm.group_srl) AS grp_srl, ba.academy_id
FROM member_group_member AS mgm
LEFT JOIN member AS m ON mgm.member_srl = m.member_srl
LEFT JOIN branch_academy as ba ON mgm.member_srl = ba.member_srl
";
$search[] = " ( ba.director_id LIKE ? OR m.user_id LIKE ? OR ba.academy_name LIKE ? OR ba.director_name LIKE ? OR m.nick_name LIKE ? OR ba.director_phone LIKE ? OR m.phone_number LIKE ? ) ";
$params[] = '%'.$obj->search_keyword.'%';
$params[] = '%'.$obj->search_keyword.'%';
$params[] = '%'.$obj->search_keyword.'%';
$params[] = '%'.$obj->search_keyword.'%';
$params[] = '%'.$obj->search_keyword.'%';
$params[] = '%'.$obj->search_keyword.'%';
$params[] = '%'.$obj->search_keyword.'%';
$obj->use_yn = $obj->use_yn ? $obj->use_yn : 'Y';
if($obj->use_yn != 'A') {
$search[] = " ba.use_yn = ? ";
$params[] = $obj->use_yn;
}
$params1[] = "%,5%";
if($obj->group_srl) {
$search[] = " 1 ";
$search1 = " AND grp_srl LIKE ? ";
$params1[] = "%".$obj->group_srl."%";
}
if(count($search)) {
$sql .= " WHERE ". implode(' AND ', $search);
}
$sql .= " GROUP BY mgm.member_srl, m.regdate, ba.academy_id ";
$sql .= " HAVING grp_srl LIKE ? ". $search1;
$params3 = array_merge($params, $params1);
$stmt = $oDB->prepare($sql);
$stmt->execute($params3);
$row = $stmt->fetchAll();
$stmt->closeCursor();
$total = count($row);
if(!$obj->page) $obj->page = 1;
$obj->page_count = 10;
if(!$obj->list_count) $obj->list_count = 20;
$offset = ( $obj->page - 1 ) * $obj->list_count;
if($obj->page) $obj->page = (int) $obj->page;
$obj->total_page = (int)ceil($total / $obj->list_count);
if($offset == 0) $params3[] = 0;
else $params3[] = $offset;
$params3[] = $obj->list_count;
$sql .= " ORDER BY m.regdate DESC LIMIT ?, ?";
$stmt2 = $oDB->prepare($sql);
$stmt2->execute($params3);
$teacher_list = $stmt2->fetchAll();
$stmt2->closeCursor();
$sql문도
SELECT mgm.member_srl, m.regdate, GROUP_CONCAT(mgm.group_srl) AS grp_srl, ba.academy_id
FROM member_group_member AS mgm
LEFT JOIN member AS m ON mgm.member_srl = m.member_srl
LEFT JOIN branch_academy as ba ON mgm.member_srl = ba.member_srl
WHERE ( ba.director_id LIKE ?
OR m.user_id LIKE ?
OR ba.academy_name LIKE ?
OR ba.director_name LIKE ?
OR m.nick_name LIKE ?
OR ba.director_phone LIKE ?
OR m.phone_number LIKE ?
) AND ba.use_yn = ? AND 1
GROUP BY mgm.member_srl, m.regdate, ba.academy_id
HAVING grp_srl LIKE ? AND grp_srl LIKE ?
ORDER BY m.regdate DESC
LIMIT ?, ?
이렇게 생성이 되는 것을 확인했습니다.
$params3 에는 데이터가 정상적으로 들어가는것을 확인했습니다.
Array ( [0] => %이% [1] => %이% [2] => %이% [3] => %이% [4] => %이% [5] => %이% [6] => %이% [7] => Y [8] => %,5% [9] => %18817% [10] => 0 [11] => 20 )