私はOOP PHPを使用して新しく、クラスメソッドを使用してデータベースでクエリを行う方法を理解するのに苦労しています。ここに私のコードと私は解決する方法を知らないエラーがあります。OOP phpでのクエリーエラーの取得
「私は、接続変数を宣言したが、私はそれが
Notice: Undefined variable: connection
Fatal error: Call to a member function query() on a non-object in
をdb.php
class DbConnector {
private $serverName;
private $userName;
private $password;
private $dbName;
private $connection;
public function __construct(){
$this->serverName = "localhost";
$this->userName = "root";
$this->password = "attl";
$this->dbName = "oop";
$this->connection = new mysqli($this->serverName, $this->userName, $this->password, $this->dbName);
if ($this->connection->connect_error) {
$this->connection = die("Connection failed: " . $this->connection->connect_error);
}
}
public function getConnection(){
return $this->connection;
}
}
のindex.php
include('../queries/db.php');
class Users{
private $connection;
public function __construct(){
$con = new DbConnector();
$connection = $con->getConnection();
}
public function getUsers(){
$sql = $connection->query("SELECT * FROM login");
while($getUsers = $sql->fetch_array()){
echo $getUsers['username'];
}
}
}
$user = new Users();
return $user->getUsers();
@hanky panky –
オリジナルの質問は「エラーが発生しました」とのリンクがありますNotice:未定義の変数:C:\ xampp \ htdocs \ oop \ views \ index.php on line 13'だからimho重複フラグは、それにスポットがすぐに解決策をあなたに役立つことができます –
あなたはどのようなエラーがありますか? –