2010-07-07 151 views
0

我有以下函數,它使查詢能夠以where條件運行。但似乎沒有產生任何結果。PDO查詢不打印任何東西

我可以連接到數據庫,所以不是問題。

任何幫助,將不勝感激。

public function executewhereQuery(Array $tableData, $fieldType, $table, $tableField){ 

    $dbh = $this->connect(); 

    try{  
     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     /*** fetch into an PDOStatement object ***/ 
     $stmt = $dbh->prepare("SELECT * FROM ".$table." 
     WHERE ".$tableField." = :fieldValue"); 

     if($fieldType=='int'){ 
      $stmt->bindParam(':fieldValue', $fieldValue, PDO::PARAM_INT); 
     } 
     else{ 
      $stmt->bindParam(':fieldValue', $fieldValue, PDO::PARAM_STR); 
     } 
     $stmt->execute($tableData); 

     /*** fetch the results ***/ 
     $results = $stmt->fetchAll(PDO::FETCH_ASSOC); 

     return $results; 

     /*** close the database connection ***/ 
     $dbh = null; 
    } 
    catch(PDOException $e){ 
     echo $e->getMessage(); 
    } 
} 

我使用下面的調用該函數:

$mydb = new Dbpdo_Database(); 


$tableData = Array('fieldValue' => 1); 

$table = 'news'; 
$tableField = 'newsID'; 
$fieldType = 'int';   

$results = $mydb->executewhereQuery($tableData, $fieldType, $table, $tableField); 

foreach ($results as $row){ 
echo $row['newsTitle'].'-'.$row['newsText1'].'<br />'; 
} 

回答

0

嘗試呼應的SQL查詢,以確保您的$table$tableField變量如預期。在上面的任何內容中,我都看不到任何會導致返回結果的情況,除非數據庫中沒有符合指定條件的數據。

1

你的問題似乎是$fieldValue沒有設置任何地方。您正在傳遞一個具有fieldValue密鑰的關聯數組。

此外,將$dbh設置爲null不會關閉數據庫連接,它只會銷燬PDO實例。無論哪種方式,該行都不會執行,因爲它遵循return聲明。