2011-04-28 61 views
0

我有以下代碼,我正在使用檢查數據庫上的約束(對於一個類)。 ü想獲得查詢返回的行數和我不斷收到就行了同樣的錯誤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 2011-04-28 20:07:25

+0

我認爲它確實返回一個空值,這就是爲什麼它不工作。 – Paul 2011-04-28 20:32:43

回答

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

$ires1不是Object,而是boolean,如PHP PDOStatement::rowcount文檔中所述。

雖然警告,從PHP.net網站:

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.

有你有太多的建議解決方案:

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."

我不知道,我不能找到方法的信息numRows,這就是我所能做到的。祝你好運!

+0

謝謝!這實際上有幫助。 – Paul 2011-04-28 20:32:08

+1

@保羅,如果它有幫助,那麼你至少應該添加一個_upvote_這個答案,如果它最終回答你的問題,然後將這個答案標記爲接受的答案 – Oerd 2011-04-28 21:28:03