PDO尝试检索数据库表时出错

时间:2014-04-25 13:25:19

标签: php mysql sql database pdo

所以当我试图从我的数据库中获取表时,我一直收到错误,这是错误:

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

我使用类中的函数连接到数据库,然后从数据库中进行选择,如下所示: CarsDb.class.php(登录信息来自__construct函数。)

    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();
    }
}

我的表格中显示的信息如下:

        <?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>

我只是将上面的相关代码缩短了,你会注意到那里的一些javascript工作正常接受由于顶部的错误我无法打印它,道歉对于糟糕的编码或不良做法,我只做了大约一个月这样做,所以我对此非常陌生,感谢任何帮助过的人。

1 个答案:

答案 0 :(得分:1)

您没有从数据库中请求任何内容,您只是创建一个语句。将其添加到select()函数中。

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