這是我目前的數據庫類:創建一個PHP PDO數據庫類,麻煩的OOP
class Database {
private $db;
function Connect() {
$db_host = "localhost";
$db_name = "database1";
$db_user = "root";
$db_pass = "root";
try {
$this->db = new PDO("mysql:host=" . $db_host . ";dbname=" . $db_name, $db_user, $db_pass);
} catch(PDOException $e) {
die($e);
}
}
public function getColumn($tableName, $unknownColumnName, $columnOneName, $columnOneValue, $columnTwoName = "1", $columnTwoValue = "1") {
$stmt = $this->db->query("SELECT $tableName FROM $unknownColumnName WHERE $columnOneName='$columnOneValue' AND $columnTwoName='$columnTwoValue'");
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $results[0][$unknownColumnName];
}
}
我嘗試使用下面的代碼來運行它:
$db = new Database();
$db->Connect();
echo $db->getColumn("Sessions", "token", "uid", 1);
和我出現以下錯誤:
PHP Fatal error: Call to a member function fetchAll() on a non-object in /Users/RETRACTED/RETRACTED/root/includes/Database.php on line 19
任何想法是怎麼回事?由於
在一個側面說明,我將通過結合使用PDO的準備語句變量,而不是直接包含它們。例如,' - > prepare('SELECT?FROM?WHERE ...') - > execute(func_get_args());'。 –
謝謝奧斯汀,我一定會在將來使用準備好的陳述。 – User2013
@AustinBrunkhorst你不能使用DB標識符(表/列名)的參數綁定,只有值 – Phil