Daryl Smith
Daryl Smith

Reputation: 19

PDO Error when trying to retrieve table for database

So I keep getting an error when trying to get my table from my database, here is the error:

object(PDOStatement)#3 (1) { ["queryString"]=> string(18) "SELECT * FROM cars" }

I'm using a function within in a class to conned to the database and then to select from the database which looks like this: CarsDb.class.php (login information comes from __construct function.)

    public function connect($db = "daryl") {
    try {
        $this->db = new PDO("mysql:host=$this->host;dbname=$db", $this->user, $this->pass);
    } catch (PDOException $error) {
        echo $error->getMessage();
    }
}
public function select($array) {
    try {

        $result ="SELECT * FROM cars";
        $array = $this->db->query($result);

        var_dump($array);

    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

and my table to put this information looks like this:

        <?php
        include ('CarsDb.class.php');
        $db = new CarsDb();
        $db->connect();
        $db->select($array);
        var_dump($array);        




    ?>
    <h1 align="center">Database of Cars.</h1>
    <form method="POST" >
        <table class="sortable">
            <thead>
                <tr>
                    <th id="makehead">Make </th>
                    <th id="modelhead">Model </th>
                    <th id="idhead">Delete </th>
                </tr>
            </thead>
            <tbody>
                <?php
                $i = 0;
                while ($row = mysql_fetch_array($db->select())) {
                    $i++;
                    echo '<tr>';
                    if ($i % 2) {
                        echo '<td class="make">' . $row['make'] . '</td>';
                        echo '<td class="model">' . $row['model'] . '</td>';
                        echo '<td class="id"><input type="checkbox" name="id" value="' . $row['id'] . '">' . $row['id'] . '</td>';   
                    } else {
                        echo '<td class="makelight">' . $row['make'] . '</td>';
                        echo '<td class="modellight">' . $row['model'] . '</td>';
                        echo '<td class="idlight"><input type="checkbox" name="id" value="' . $row['id'] . '">' . $row['id'] . '</td>';                           
                    }
                    echo '</tr>';
                }
                ?>
            </tbody>
            <td>
                <input Onclick="return ConfirmDelete();" name="delete" type="submit"     id="delete" value="Delete"></input>
            </td>
        </table></form>

    <a href= 'CarsWebpage.html'><br>Go back to the beginning.</a>
<?php
mysql_close($con);
?>
</body>
</html>

I've only put the relevant bit of code above to shorten it, you will notice some javascript in there which is working fine accept I can't get it to print due to error at the top, apologies for bad coding or bad practices as I have only been doing this for about a month so I'm very new to this, thanks to anyone who helps.

Upvotes: 0

Views: 61

Answers (1)

Gerald Schneider
Gerald Schneider

Reputation: 17797

You not requesting anything from the database, you are only creating a statement. Add this to your select() function.

$stmt = $this->db->query($result);
$array = $stmt->fetchAll();
var_dump($array);

Upvotes: 1

Related Questions