0
您好,我有一個問題PDO連接。我conn.php是PDO連接:調用成員函數prepare()null
<?php
define('host', 'localhost');
define('host_user', 'root');
define('host_pass', '');
define('host_db', 'testdb');
class Database {
public $conn;
public function Connect() {
try {
$conn = new PDO("mysql:host=" . host . ";dbname=" . host_db, host_user, host_pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection error:" . $e->getMessage();
}
}
}
?>
當我使用這個
$dbClass = new Database();
$stmt = $dbClass::Connect()->prepare("SELECT * FROM user_posts");
返回以下錯誤:
Call to a member function prepare() on null
爲什麼會發生?
感謝它的工作原理! – user3227899
@ user3227899不客氣:-)請記住選擇我的答案,如果它可以幫助您解決問題。 – BeetleJuice
選擇?什麼意思? – user3227899