2014-05-09 2 views
0

안녕하세요. 이렇게하려고했습니다 ...PHP에서 SQL 행 결과의 유효성 확인

쿼리가 0, 1 또는 2로 다른 작업을 수행 할 때.

SELECT status FROM event_auth WHERE perm = true AND id_event = 1 

하고

status 
    -------- 
    0 
    0 
    2 

다음 나는 그것이

SELECT status FROM event_auth WHERE perm = true AND id_event = 1 GROUP BY status 

    status 
    -------- 
    0 
    2 

그것은 3 개 이상 열 수 있습니다 않았다 반환은, 난 그냥 그 행을 싶어. 나는이 문제를 확인하려면 ... 의 PHP 코드 ...

if ((rows == 1) == 1){ 
    // do something 
    } 
    if ((rows => 1) == 0){ 
    // do something 
    } 
    if (rows > 1) == 2{ 
    //do something 
    } 
    if 

(explaning) 내 모든 권한 부여가

0 = pending, 
    1 = accepted, 
    2 = rejected. 

0 또는 그 중 한 경우 권한 부여 운동 모듈이 0이면 아무것도하지 마십시오.

둘 중 하나에 2를 넣으면 거부하십시오. 그들 모두는 하나를 넣어 경우

,

내가 할 수있는 일을 존재 뭔가 ... (다른 테이블에서 상태를 변경)을합니까?

감사합니다.

+0

당신이 뭘 하려는지? 나는 이해하지 못한다. '0','1'','2''을 얼마나 많이 꼽고 싶습니까? –

+2

당신의 필요를 이해하기가 어렵지만, 상태 당 행 수를보고하는'SELECT status, COUNT (*) FROM event_auth WHERE ... GROUP BY status'라는 대답은 집계'COUNT()'에 대한 답입니다. –

+0

그냥 내 쿼리 그룹에 의해 반환 된 행에 의해 모든 2 -> 아무 것도하지 않으면 .... 알고 싶은 경우 .... 내 쿼리 그룹에 의해 반환되는 경우에만 1 행 값을 1 -> 뭔가, 그리고 내 쿼리 그룹 by rows rows => 1 wait = 행까지 1 – StriderWaR

답변

1

다음은 내가 필요로하는 것으로 생각되는 테스트 된 코드입니다.

$ eventId 변수를 변경하여 테스트하십시오.

는 약간의 변화가 있습니다

쿼리는 다른 'status'es의 수를 반환 실패/수용을 테스트 할 때이 쉬워집니다.

무엇이 발생했는지 결정한 이유를 표시합니다. 여기

출력은 공급 된 테스트 데이터를 사용하고 있습니다 :

boolean false 
string 'This event is Rejected: 1 : counts: 1' (length=37) 
string 'P:\developer\xampp\htdocs\testmysql\index.php' (length=45) 

코드 :

<?php 
    // assume that the STATUS column MUST be one of these values 
    define('PENDING', 0); 
    define('ACCEPTED', 1); 
    define('REJECTED', 2); 

    // connect to the database 
    $dsn = 'mysql:host=localhost;dbname=testmysql'; 
    $username = 'test'; 
    $password = 'test'; 
    $options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', 
    ); 
    $db = new PDO($dsn, $username, $password, $options); 
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 


    // testing -- change this id to ensure it works 
    $eventId = 2; 
    // ------------- 

    // return the counts for each status - will make it easier to check later 
    $sql = " SELECT ea.status as `status`, COUNT(ea.status) AS status_count " 
      ." FROM event_auth AS ea " 
      ." WHERE ea.perm = TRUE AND ea.id_event = :eventId " 
      ." GROUP BY ea.status " 
      ." ORDER BY ea.status DESC "; 

    $stmt = $db->prepare($sql); 
    $allOk = $stmt->execute(array(':eventId' => $eventId)); 

    $statusList = $stmt->fetchAll(PDO::FETCH_ASSOC); // column names only 

    // assume that we will NOT accept the event 
    $acceptEvent = false; 
    $theReason = ''; // why we did what we did ... 

    // check the return statuses and counts 
    foreach($statusList as $details) { 
     if ( $details['status'] == REJECTED 
      && $details['status_count'] >= 1) { // will stop the loop 
      $theReason = "This event is Rejected: {$eventId} : counts: {$details['status_count']}"; 
      $acceptEvent = false; 
      break; 
     } 

     if ( $details['status'] == PENDING 
      && $details['status_count'] >= 1) { // will stop the loop 
      $theReason = "This event is Pending: {$eventId} : counts: {$details['status_count']}"; 
      $acceptEvent = false; 
      break; 
     } 

     // this never stops the loop 
     if ( $details['status'] == ACCEPTED 
      && $details['status_count'] >= 1) { 
      $theReason = "This event is Accepted: {$eventId} : counts: {$details['status_count']}"; 
      $acceptEvent = true; 
     } 
    } 

    var_dump($acceptEvent, $theReason, __FILE__); 
+0

고마워, 나는 약간의 변경을했다. 그러나 그것은 내가 찾고 있었던 것이다. – StriderWaR

+0

@StriderWaR, 도와 줘서 기쁩니다. –

관련 문제