테스트 사이트 - 개발 중인 베타 버전입니다

두개의 쿼리를 실행하는데 처리되지 않는데... 잘못 처리한건가요? 채택완료

더블피디 8년 전 조회 1,881

db는 정상적으로 연결되는것은 확인됨...

 

3.DB table 쿼리 실행 부분에 

아래와 같이 쿼리 두개를 실행시키는 작업을 하는데요 echo 출력이 되지 않고, 실제 처리도 안되고..

 

최종적으로 실행되어야 하는 $sql 마지막 wr_id 값이 고정되지 않고,

실행하는 시점에 wr_hit값이 제일 높을 wr_id를 추출하여 특정필드(wr_5) 업데이트 하려합니다. 

 

$select 를 실제 실행하면

mysql> select wr_id from g5_write_info1 where wr_is_comment=0 order by wr_hit desc limit 1;

+-------+

| wr_id |

+-------+

|     4 |

+-------+

1 row in set (0.00 sec)

 

 

$select  에 위에서 쿼리한 4가 입력되도록 하려는데

$sql="update g5_write_info1 set wr_5=1 where wr_is_comment=0 and wr_id ='$select' "; 

 

잘못 처리한건가요?  

 

</p><p> </p><p><?php</p><p> </p><p>// 1. 연결 : mysql_connect(호스트명, 아이디, 비밀번호)</p><p>$conn=mysql_connect('localhost', 'root', 'eoe.e2'); //db 연결부분</p><p> </p><p>// 2. DB 선택 : mysql_select_db(해당 db명, $conn)</p><p>$db=mysql_select_db("test", $conn);</p><p> </p><p>if($db)</p><p>   echo "db 연결성공";</p><p>else</p><p>   echo "db 연결 실패";</p><p><span style="font-size: 11pt;"> </span></p><p><span style="font-size: 11pt;">// 3. DB에 table 쿼리(query 질의).</span></p><p>$select ="select wr_id from g5_write_info1 where wr_is_comment=0 order by wr_hit desc limit 1 "; // 실행 시점에 wr_hit값이 가장 높은거 한건 고르기</p><p><span style="font-size: 11pt;">$sql="update g5_write_info1 set wr_5=1 where wr_is_comment=0 and wr_id ='$select' ";  // 실행되는 시점에 wr_5필드 업데이트</span></p><p> </p><p> </p><p>//echo "$sql";</p><p>//mysql_query($sql, $conn)  db에 질의 수행.</p><p>mysql_query($sql_1st, $conn);</p><p> </p><p>echo "<script> alert('$sql'); </script>";  // 수행되는 쿼리에 대한 확인목적</p><p>?></p><p>


댓글을 작성하려면 로그인이 필요합니다.

답변 4개

채택된 답변
+20 포인트
잉끼s
8년 전

</p><p><code class="variable">//$select</code> <code class="plain">=</code><code class="string">"select wr_id from g5_write_info1 where wr_is_comment=0 order by wr_hit desc limit 1 "</code><code class="plain">; </code><code class="comments">// 실행 시점에 wr_hit값이 가장 높은거 한건 고르기</code>
$sql="
    update g5_write_info1 set 
    wr_5=1 
    where wr_is_comment=0 
    and wr_id =(
        select wr_id from g5_write_info1 where wr_is_comment=0 order by wr_hit desc limit 1
    ) 
";  </p><p>

 

이렇게 하세요. 

 

 

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

포이치
8년 전

일반적인 서브쿼리로 업데이트를 진행시 위에 서브쿼리 적어주신분 내용처럼하면

오류가 나죠..

업데이트 대상을 서브쿼리로 사용하게되니... (업데이트 대상과 서브쿼리 대상이 동일)

 

UPDATE g5_write_info1 

SET  wr_5=1

where  wr_id=(select * from (SELECT wr_id from g5_write_info1 where wr_is_comment=0 order by wr_hit desc limit 1) as x)  

 

이렇게 하셔야 될듯 합니다.


그리고 하시려고 하시는것처럼 하셔도 되는데
아무리봐도 쿼리실행 구문이 안보이네요..

$select[wr_id] 에 값이 들어가있다면 문제될건 없어보이는데
코드상 쿼리를 fetch 하는 부분이 없습니다.


로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

u
8년 전

$select ="select wr_id from g5_write_info1 where wr_is_comment=0 order by wr_hit desc limit 1 "; </p><p>$result = mysql_query($select, $conn);</p><p>$row = mysql_fetch_array($result);</p><p>$wr_id = $row['wr_id'];</p><p> </p><p>$sql="update g5_write_info1 set wr_5=1 where wr_is_comment=0 and wr_id ='$wr_id' ";

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

k
8년 전
$select ="select wr_id from g5_write_info1 where wr_is_comment=0 order by wr_hit desc limit 1 "; // 실행 시점에 wr_hit값이 가장 높은거 한건 고르기
17

$sql="update g5_write_info1 set wr_5=1 where wr_is_comment=0 and wr_id =($select) ";


로 해보세여 subquery 는 스트링으로 처리하시면 안댈듯 


로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인