2011-04-28 3 views
0

데이터베이스 (클래스)에 대한 제약 조건을 확인하기 위해 다음 코드를 사용하고 있습니다. U는 쿼리에 의해 반환되는 행의 수를 얻기 위해 노력하고 내가 라인에서 같은 오류가 계속PHP mySQL numRows() 사용 및 오류?

$count1= $ires1->numRows(MDB2_FETCHMODE_ASSOC); 

오류 :

> Call to a member function numRows() on a non-object 

내가 내 다른 때문에 내 머리를 잡아 당겨 봤는데 이것과 비슷한 기능은 잘 작동하지만이 기능은 작동하지 않는 유일한 기능입니다. 이 중 하나에서 눈에 띄는 것이 있습니까?

인수 $ DB를 내 데이터베이스에 바로 연결입니다, pno는 정수이며, essn 텍스트입니다 .. 그래서 내가 뭘 잘못 모르겠어요 ..

<?php 
function submitCheck($db){ 
    $essn= $_POST['essn']; 
    $pno=$_POST['pno']; 

    $query1 = "select * from works_on where pno=? and essn=?"; 
    $types1 = array('integer','text'); 
    $stmt1 = $db->prepare($query1, $types1, MDB2_PREPARE_MANIP); 

    if (MDB2::isError($stmt1)) { 
     print("bad prepared statement:" . $stmt->getMessage()); 
    } 

    $queryargs1 = array($pno, $essn); 
    $ires1 = $stmt1->execute($queryargs1); 
    $count1= $ires1->numRows(MDB2_FETCHMODE_ASSOC); 
    //print("The project number entered was $count1[pno]"); 
    if(!(count($count1)==0)){ 
     print("The employee is already part of this project! If you want to update the hours, please select update!"); 
     return false; 
    } 
    return true; 
} 
?> 
+1

'$ ires1'의 값은 무엇입니까? 쿼리가 실패하고'$ stmt1-> execute'가 어떤 오류 값을 대신 반환 할 수 있습니까? ('false' 또는'null'과 같음) – Halcyon

+0

null 값을 리턴한다고 생각합니다. 이것이 작동하지 않는 이유입니다. – Paul

답변

2
$count1 = $stmt1->rowCount(); 

$ires1Object이 아니지만 PHP PDOStatement::rowcount 설명서에 명시된대로 boolean입니다.

PHP.net 사이트에서 비록 경고 :

For most databases, PDOStatement::rowCount() does not return the number of rows affected by a SELECT statement. Instead, use PDO::query() to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use PDOStatement::fetchColumn() to retrieve the number of rows that will be returned. Your application can then perform the correct action."

내가 몰랐던 나는 방법에 대한 정보를 찾을 수 없습니다 :이

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

당신이 너무 자신의 제안 솔루션을 numRows, 내가 할 수있는 한 그렇게까지. 행운을 빕니다!

+0

감사! 이것은 실제로 도움이되었습니다. – Paul

+1

@Paul, 도움이된다면이 답변에 _upvote_를 추가해야합니다. 궁극적으로 질문에 답변 한 경우이 대답을 허용 대답으로 표시하십시오 – Oerd