我有一個連接類文件,它允許我的其他類「函數」連接到我的MySQL數據庫。但是,當我執行MySQL查詢時,它僅返回Array()
。我選擇的數據其實就在那裏(我查過)。問題是什麼?PDO查詢返回空
Connection.php
<?php
class Connection extends PDO {
private $username;
private $password;
private $database;
private $hostname;
public function __construct($hostname, $username, $password, $database) {
$this->hostname = $hostname;
$this->username = $username;
$this->password = $password;
$this->hostname = $hostname;
try {
parent::__construct("mysql:host=" . $this->hostname . ";dbname=" . $this->database, $this->username, $this->password);
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
}
?>
的functions.php
<?php
require_once "Connection.php";
class Functions {
private $connection;
public function __construct() {
$this->connection = new Connection("127.0.0.1", "xxx", "xxx", "xxx");
}
public function sqlFetchAssoc($query) {
$sth = $this->connection->prepare($query);
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
}
$functions = new Functions();
$row = $functions->sqlFetchAssoc("SELECT * FROM chatlogs WHERE id = 70");
print_r($row);
?>
你應該在那裏添加更多的錯誤檢查。看看你是否在某處發生錯誤。 –
你爲什麼不直接使用PDO對象呢? –
http://www.php.net/manual/en/pdo.errorinfo.php –