0

I am creating a OO CRUD system and I am failing to see why I am not receiving any rows back. The connection works, data is in the table, and when i hardcode the table name into the query it works. It does not work however when placeholders are used. Any help greatly appreciated.

index.php

<html>
<head>
<title>Content Management System</title>
<?php   include'class.DB.php';      ?>

</head>

<body>
<table style="width:100%">
<tr><td>ID</td><td>First Name</td><td>Last Name</td><td>Email</td></tr>
<tr></tr>
<tr></tr>

</table>
    <?php


    $customer = new DB();
    $table = $customer->getTable('customer');

    foreach($table as $row){
        echo $row->id;
    }



    ?>
</body>
</html>

class.DB.php

include 'class.config.php';

class DB  {


    private $db;

    function __construct(){

        $this->db = new PDO('mysql:host=localhost;dbname=cms;charset=utf8', 'hidden', 'hidden');

        if($this->db){
            return $this->db;
        }
        echo 'error';
    }

    function getTable($name){

        $stmt = $this->db->prepare('SELECT * FROM :table');
        $stmt->bindParam(':table',$name,PDO::PARAM_STR);
        $stmt->execute();

        $results = $stmt->fetchAll();
        echo $stmt->rowCount();

        return $results;

    }


}
Joe Smith
  • 735
  • 1
  • 5
  • 4

0 Answers0