2012-09-14 2 views
0

나는 이벤트 프로젝트에서 작업 중이며 프로필 페이지에서 호스팅 한 내 사용자의 이벤트를 보여주고 싶지만 이벤트 목록을 보았습니다.PHP PDO 이벤트 목록에 대한 쿼리

다른 것들은 잘 작동합니다.

if(isset($_GET["id"])) { 
$id = intval($_GET["id"]); 
if(!empty($id)) { 

try { 
$pq = "SELECT * FROM `users` WHERE `id` = :id"; 
$pq_check = $db->prepare($pq); 
$pq_check->bindParam(':id', $id, PDO::PARAM_INT); 
$pq_check->execute(); 
$ac = $db->query("SELECT FOUND_ROWS()")->fetchColumn(); 
} 
catch(PDOException $e) { $log->logError($e." - ".basename(__FILE__)); 
} 


// i'm fetching the user info and showing them name age gender exc. , no problem with here 


echo "Events That User Hosted :\n"; 

// here is the place i have problem 

$eq = "SELECT * FROM `events` WHERE `host_id` = :id"; 
$eq_check = $db->prepare($eq); 
$eq_check->bindParam(':id', $id, PDO::PARAM_INT); 
$eq_check->execute(); 
$foo = $db->query("SELECT FOUND_ROWS()")->fetchColumn(); 
if(!empty($foo)) { 
$_loader = true; 
$fetch = $eq_check->fetch (PDO::FETCH_ASSOC); 
} 

while($fetch = $eq_check->fetch (PDO::FETCH_ASSOC)){ 
if ($fetch == NULL) { 
break; 
} 

$event_id = $fetch['event_id']; 
$event_name = $fetch['event_name']; 
$link = "https://www.mywebsite.com/e/$event_id"; 

echo "<a target=\"_blank\" href=\"$link\"><li>$event_name</li></a>"; 

} 

} 
} 

+1

실제 문제 *는 무엇입니까? SQL 오류, 예상 한 데이터가 아닙니다. – Fluffeh

+0

오류를주지는 않지만 인쇄하지 않는 경우 –

답변

1

하나의 문제는 결과 집합의 첫 번째 행을 가져올 것입니다 당신을 감사하고 당신이 그것을 던져 :

if(!empty($foo)) { 
    $_loader = true; 
    $fetch = $eq_check->fetch (PDO::FETCH_ASSOC); 
} 

/* above you have fetched the first row but that value gets overwritten 
    directly after that: */ 

while ($fetch = $eq_check->fetch (PDO::FETCH_ASSOC)) { 

/* $fetch now contains the second row of the result set, if it exists... */ 

여기

는 코드입니다 편집 : 코드를 정리하고 오류 처리를 추가하고 어떤 일이 발생하는지 확인하십시오.

try 
{ 
    $eq = "SELECT * FROM `events` WHERE `host_id` = :id"; 
    $eq_check = $db->prepare($eq); 
    $eq_check->bindParam(':id', $id, PDO::PARAM_INT); 
    $eq_check->execute(); 

    while($fetch = $eq_check->fetch (PDO::FETCH_ASSOC)) 
    { 
    $_loader = true; 

    $event_id = $fetch['event_id']; 
    $event_name = $fetch['event_name']; 
    $link = "https://www.mywebsite.com/e/$event_id"; 

    echo "<a target=\"_blank\" href=\"$link\"><li>$event_name</li></a>"; 
    } 
} 
catch(PDOException $e) 
{ 
    $log->logError($e." - ".basename(__FILE__)); 
} 
+1

추가 : 'while ($ fetch = $ eq_check-> fetch (PDO :: FETCH_ASSOC)) { if ($ fetch == NULL) { break; }'- if는 무의미합니다. – mrmryb

+0

네, 그 때문에 첫 번째 이벤트에 대해 더미 콘텐트를 가져 왔지만 그 이후에 다른 이벤트가 있습니다. 그러나 역시 그들을 나열 할 수는 없습니다 .. –

+0

@mrmryb 완전히 진실이라고 말하면 두 번째 쿼리 '$ foo = $ db-> query ("SELECT FOUND_ROWS()") -> fetchColumn();'이 불필요한 경우,'execute' 문 바로 다음에 행을 가져 오기 시작할 수 있습니다. – jeroen