[英]PDO Query not printing anything
I have the following function which enables a query to run with a where condition. 我具有以下功能,该功能使查询可以在where条件下运行。 But it seems not to be producing any results.
但这似乎没有产生任何结果。
I can connect to the database so that is not an issue. 我可以连接到数据库,所以这不是问题。
Any help would be appreciated. 任何帮助,将不胜感激。
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();
}
}
I am calling the function using the following: 我使用以下命令调用该函数:
$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 />';
}
Your problem seems to be that $fieldValue
isn't set anywhere. 您的问题似乎是
$fieldValue
没有设置在任何地方。 You're passing an associative array that has the fieldValue
key. 您正在传递具有
fieldValue
键的关联数组。
Also, setting $dbh
to null
doesn't close the DB connection, it only destroys the PDO instance. 同样,将
$dbh
设置$dbh
null
不会关闭数据库连接,只会破坏PDO实例。 And either way, that line wouldn't execute because it follows a return
statement. 无论哪种方式,该行都不会执行,因为它遵循
return
语句。
Try echoing your SQL query to ensure your $table
and $tableField
variables are as expected. 尝试回显SQL查询,以确保
$table
和$tableField
变量符合预期。 I can't see anything in the above that would cause no results to be returned, other than there's no data in your database matching the specified condition. 除了您的数据库中没有符合指定条件的数据外,我在上述内容中看不到任何会导致不返回任何结果的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.