I have a class that contains methods that query the database for information and then return a variable which value is the prepared version of the sql query, after "execute()
" and "setFetchMode()
", the function returns that same variable.
In another file I create an object of that class, and call that method which I assign as the value of another variable so that I can fetch()
it on a while loop. The problem is that all of that happens without any apparent errors but the data that I request from the database is not retrieved.
<?php
class DatabaseContent{
private $sql = "SELECT * FROM :table ";
public function fetchAllRows($table, $rowOrder, $direction, $conn){
$this->sql .= "ORDER BY :roworder :direction ";
$q = $conn->prepare($this->sql);
$q->execute(array(':table'=>$table, ':roworder'=>$rowOrder, ':direction'=>$direction));
$q->setFetchMode(PDO::FETCH_ASSOC);
return $q;
}
And this is the file where I create the object
<?php
$table = "topics";
$rowOrder = "topic_id";
$direction = "ASC";
$q = new DatabaseContent;
$n = $q->fetchAllRows($table, $rowOrder, $direction,$conn);
while($row = $n->fetch()):
echo '<a href="categoria/categoria.php"><li>'.$row['topic_name'].'</li></a>';
endwhile;
?>