0

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;
                ?>      
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Yuran Pereira
  • 258
  • 3
  • 11
  • I think `$q->setFetchMode` should be called before `$q->execute` – JC Sama Feb 21 '15 at 22:46
  • You're breaking the first and foremost rule in binding. **You cannot bind tables and/or columns.** – Funk Forty Niner Feb 21 '15 at 22:48
  • Add `setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)` and you'll see what I mean. – Funk Forty Niner Feb 21 '15 at 22:49
  • @Fred-ii- I really didn't know that you cannot bind tables and columns, so I couldn't search for something specific, that's why I didn't know that the answer for this already existed. I understand you had to flag it as a duplicate, but was it really necessary to vote it down??? Now I can't ask more questions cause I got more than two down votes. Sometimes you guys here on stackoverflow are really harsh with other people. But anyway thanks for the answer it was useful in some way. – Yuran Pereira Feb 24 '15 at 09:30
  • When it comes to questions like these, they are closed with duplicates because Stack wants us to do that. Otherwise, the questions/answers would just keep piling up. If you feel the question should be reopened, you can flag your question and let the moderators know. I don't run Stack, I merely go by their rules. Don't always assume downvotes coming from a person in particular. I rectified that, but remember: There is/was more than one person who visited your question. – Funk Forty Niner Feb 24 '15 at 12:32

0 Answers0