[英]PDO Query not printing anything
我具有以下功能,該功能使查詢可以在where條件下運行。 但這似乎沒有產生任何結果。
我可以連接到數據庫,所以這不是問題。
任何幫助,將不勝感激。
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 />';
}
您的問題似乎是$fieldValue
沒有設置在任何地方。 您正在傳遞具有fieldValue
鍵的關聯數組。
同樣,將$dbh
設置$dbh
null
不會關閉數據庫連接,只會破壞PDO實例。 無論哪種方式,該行都不會執行,因為它遵循return
語句。
嘗試回顯SQL查詢,以確保$table
和$tableField
變量符合預期。 除了您的數據庫中沒有符合指定條件的數據外,我在上述內容中看不到任何會導致不返回任何結果的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.