sql쿼리관련
본문
게시판에서 내가 작성한 글들을 불러오려고 쿼리를 찾다가 union을 사용했습니다.
게시판정보 테이블에서 게시판 이름을 가져와서 for문 돌려서 게시판에 있는
게시글 숫자를 다 합치는 쿼리를 만들었습니다.
select sum(all_count.cnt) AS all_counts
from (
SELECT count(*) cnt from `g5_write_email` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_notice` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_bodo` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_review` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_snsphoto` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_snsvideo` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_report` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_magazine` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_tv` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_bfood` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_page` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_food` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_event` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_inquiry` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_pubandpop` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_wbodo` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_foodstory` where mb_id = 'ri1205'
union all SELECT count(*) cnt from `g5_write_companypoto` where mb_id = 'ri1205'
) all_count
위 쿼리를 만들어서 바로 sql에 실행을 하면
all_counts |
---|
1786 |
위처럼 결과가 나옵니다.
근데 php페이지에서
$all_row = sql_fetch($all_query);
$total_count = $row['all_counts'];
echo $all_query;해서 값을 확인하면 값이 안나오고 있어요...
혹시나해서 print_r($all_row)배열로 확인하면 Array ( [1] => 1 ) 라고만 나와요;;;
어디가 문젠지 모르겠어요;;쿼리는 실행이 되서요 ㅠㅠ
답변 4
sql_query 함수는 union을 사용하지 못하게 막아놨습니다. mysql_query를 사용해보세요
sql_fetch 도 union을 사용하지 못하게 preg_replace 로 함수에서 막혀있습니다.
$total_count = $all_row['all_counts'];
쿼리 호출후 변수명이 잘못 되어 있는걸로 확인 됩니다.
그리고 쿼리 생성이 제대로 안되고 있는듯 한데 확인이 안되서 정확히 뭐라 말씀드리기는 어렵네요
내가 작성한 글은, /bbs/new.php?mb_id=[ID]
( /bbs/new.php?mb_id=ri1205 ) 로 확인 가능합니다.
새 글 (new.php) 페이지를 활용하는 것을 추천합니다.
몇가지 참고할 만한 사항들 남깁니다.
* g5_board_new 테이블에는 최신글만 들어가는 단점이 있습니다.
저도 해본적은 없지만, 관리자 > 기본 설정 > 최근 게시물 삭제일을 30일에서 9999일등으로 변경하는 방법을 생각해 볼 수 있습니다. g5_board_new 테이블에서만 count 가능?
* https://sir.kr/bbs/board.php?bo_table=g5_tip&wr_id=2753
같은 그룹에서 전체 작성글 갯수 뽑기 함수 관련 글입니다.
제가 잠깐 테스트했을 때는 오류가 나오기는 했는데, 그래도 한번 살펴볼 수 있지 않을까 하여 말씀드립니다.
* common.lib.php 파일에서 sql_query() 시 (sql_fetch() 도 sql_query() 과정 포함)
union 차단 부분을 임시적으로 해제하여 테스트 해볼 수 있지 않을까 합니다.
(관련부분) $sql = preg_replace("#^select.*from.*[\s\(]+union[\s\)]+.*#i ", "select 1", $sql);