尝试用pdo替换过程语言但它没有显示输出只是一个空白页面

时间:2018-02-08 09:41:52

标签: php pdo

我是pdo的新手,因为我习惯使用mysql程序,所以我做了一个conn.php,其中我有数据库连接

<?php
class Connection{
private $localhost="xxxx";
private $username="xxxx";
private $dbname="xxxx";
private $password="";
public $conn="";
function __construct(){

    $this->connect();   

}

function connect(){
try {
    $this->conn = new PDO("mysql:host=$this->localhost;dbname=$this->dbname", $this->username, $this->password);
    // set the PDO error mode to exception
    $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }}
}
?>

并将其包含在另一个名为function.php

的文件中
<?php
include("conn.php");//here i'm including connection
Class Query{
    function _construct(){
        $this->select();
    }
    function select(){
$sql = "SELECT * FROM demotable";   
    $result = $conn->query($sql);
    if($result->rowCount() > 0){
        echo "<table>";
            echo "<tr>";
                echo "<th>id</th>";
                echo "<th>first_name</th>";
                echo "<th>last_name</th>";
                echo "<th>email</th>";
            echo "</tr>";
        while($row = $result->fetch()){
            echo "<tr>";
                echo "<td>" . $row['id'] . "</td>";
                echo "<td>" . $row['first_name'] . "</td>";
                echo "<td>" . $row['last_name'] . "</td>";
                echo "<td>" . $row['email'] . "</td>";
            echo "</tr>";
        }
        echo "</table>";
        // Free result set

    } else{
        echo "No records matching your query were found.";
    }
}
}
?>

但我的问题是它没有显示任何错误没有输出

0 个答案:

没有答案